Event Receiver does not fire

I wrote an event receiver for a list but it seems not firing when you add/update/delete an item.

I used the following Powershell script to see what event receivers that are attached to this list:

Add-PsSnapin Microsoft.SharePoint.PowerShell
$spWeb = Get-SPWeb -Identity http://yourWebUrl
$spList = $spWeb.Lists[“The List’s Name”]
$spList.EventReceivers | Select Name,Assembly,Type

Turned out there were some older event receivers that are still attached to it.

I then had to use the following script to delete all event receivers for this list:

Add-PsSnapin Microsoft.SharePoint.PowerShell
$spWeb = Get-SPWeb -Identity http://yourWebUrl
$spList = $spWeb.Lists[“The List’s Name”]
$numberOfEventReceivers = $spList.EventReceivers.Count
if ($numberOfEventReceivers -gt 0)
{
for( $index = $numberOfEventReceivers -1; $index -gt -1; $index–-)
{
$receiver = $spList.EventReceivers[$index] ;
$receiver.Delete()
}
}

After deleting the event receivers, deployed my receivers again and it worked like a charm!

 

Posted in SharePoint 2010, PowerShell | Leave a comment

Get all items from a list performance

Running on a list with 4,000 items. Option 1 is on average 1 second slower than Option 2.

Option 1:

using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb currentWeb = currentSite.OpenWeb(“crm”))
{
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
string result = “”;
SPList myList_TrainingRecords = currentWeb.Lists[“Personnel”];
SPListItemCollection listItemCollection = myList_TrainingRecords.Items;
foreach (SPListItem item in listItemCollection)
{
result = Convert.ToString(item[“Surname”]);
}
dt2 = DateTime.Now;
TimeSpan duration = dt2 – dt1;
lblTesting.Text = “Result: ” + result + ” ” + duration.TotalMilliseconds;
}
}

 

Option 2:

using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb currentWeb = currentSite.OpenWeb(“crm”))
{DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
string result = “”;
SPList myList_TrainingRecords = currentWeb.Lists[“Personnel”];
SPQuery qry = new SPQuery();
qry.RowLimit = 1999;
do
{
SPListItemCollection listItemCollection = myList_TrainingRecords.GetItems(qry);
foreach (SPListItem item in listItemCollection)
{
result = Convert.ToString(item[“Surname”]);
}
qry.ListItemCollectionPosition = listItemCollection.ListItemCollectionPosition;
} while (qry.ListItemCollectionPosition != null);
dt2 = DateTime.Now;
TimeSpan duration = dt2 – dt1;
lblTesting2.Text = “Result: ” + result + ” ” + duration.TotalMilliseconds; }
}

 

Posted in SharePoint 2010, Visual Studio 2010, SharePoint 2016 | Leave a comment

SharePoint 2013/2016 Branding

1. Create a html/css/javascript file using your favorite designer tool;
2. Go to SharePoint >> Site Settings >> Designer Manager;
3. Go to “Step 3 Upload design files” and Map the following location as a network drive so you can work easily with your design files: http://yoursharepointdomain.com/_catalogs/masterpage/
4. Go to “Step 4 Edit Master Pages” and “Convert an HTML file to a SharePoint master page”;
5. After it’s converted to master page, go to the mapped drive and then open the .html and .css file you uploaded; Modify them as needed.
6. Once you are happy with the result, Go to “Step 7 Publish and Apply Design”
1) “Go to the Master Page Gallery” and publish the .html file;
2) “Assign master pages to your site based on device channel”

Once it is published, you can still modify the .html and .css file.

Note:

if you want to hide the header and footer in popup/dialog pages, add class names “s4-notdlg noindex” to the header and footer.

If you want to display drop down menu in the top global navigation,
1) Go to “Site Settings >> Navigation”, Create a header and move sites/links under that header;
2) Go to the Snippet gallery, change the MaximumDynamicDisplayLevels (under Behavior) to 1. Copy and Paste the code into your masterpage.html file.

Posted in SharePoint 2016 | Tagged | Leave a comment

Create a notification banner on top of every page in a SharePoint 2010 site collection using delegate control

We wanted to create a notification banner on top of every page in a SharePoint 2010 site collection like this:

1

You could modify the master page for each site but that very cumbersome.

So we will use the “Delegate Control” to achieve this, without touching the mater pages.

Delegate Control creates a region in the page that will allow to replace the original content with our custom content. This can be handled using SharePoint feature.

In this example I am going to create user control that is going to replace the ‘GlobalNavigation’ in master page using delegate control.

The following the the v4.master page and the DelegateControl with ControlId as “GlobalNavigation” is the one we want to replace the content.

2

Steps to create Delegate Control:

1. Create empty SharePoint project in Visual Studio and deploy as a “Farm Solution“:

3

2. Right click your project name and then “Add >> SharePoint Mapped Folder…”

4

3. Choose “CONTROLTEMPLATES” and click OK:

5

4. Right click CONTROLTEMPLATES you just added and then click “Add >> New Item…”

6

5. Add a new “User Control”:

7

6. Right click your project name and add a new item:

8

7. Add a new “Empty Element”:

9

8. Now your solution explorer looks like this:

10

9. Double click “Feature1” and then change the scope from “Web” to “Site”:

12

10. Open the “Elements.xml” file and modify it so it looks like this:

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<Control Id=”GlobalNavigation” Sequence=”1000″ ControlSrc=”~/_controltemplates/UserControl1.ascx” />
</Elements>

13

11. Open the UserControl1.ascx designer view and add the following lines:

<SharePoint:CssRegistration ID=”CssRegistration3″ runat=”server” Name=”/Style Library/Custom CSS/custom.css” />

<asp:Panel ID=”pnlSiteNotifications” runat=”server” CssClass=”SiteNotifications”>

</asp:Panel>

14

Obviously you will need to have a “custom.css” file in this location: “/Style Library/Custom CSS/custom.css”

This is what I have in the custom.css file:

15

12. Create a custom list somewhere in SharePoint called “Site Notifications” and the fields are:

16

13. Open the Code View of the UserControl1.ascs.cs and insert the following line inside the “Page_Load” function:

using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb parentWeb = currentSite.OpenWeb(“Departments”))
{
SPWebCollection webs1 = parentWeb.Webs;
SPWeb currentWeb1 = webs1[“IT”];

SPWebCollection webs = currentWeb1.Webs;
SPWeb currentWeb = webs[“MelbIT”];

SPQuery query = new SPQuery();
query.Query = String.Format(@”
<Where>
<Eq>
<FieldRef Name=’Active’ />
<Value Type=’Boolean’>1</Value>
</Eq>
</Where>”, “”);

var list = currentWeb.Lists[“Site Notifications”];
SPListItemCollection listItems = list.GetItems(query);
if (listItems.Count > 0)
{
foreach (SPListItem item in listItems)
{
Literal1.Text = Convert.ToString(item[“Notification”]) + “

Please contact ” + Convert.ToString(item[“Contact”]) + “ if you have any concern. Thank you!

“;
}
}
else
{
pnlSiteNotifications.Visible = false;
}
}
}

17

14. Deploy your project:

18

15. Go to the “Site Notifications” list and add a new notification:

19

The notification should be on top of every page of the site collection. If you want to remove the notification, you can either delete the above list item or uncheck the “Active” checkbox.

Hope this tutorial helps someone.

Happy coding!

 

 

Posted in SharePoint 2010, Visual Studio 2010 | Leave a comment

How to Recover A Single Document In SharePoint

How to Recover A Single Document In SharePoint

  • How to Recover A Single Document In SharePoint

    Once upon a time at a customer site (August of last year, in fact), one of the users made some changes to an Excel document that he couldn’t roll back. Versioning was not enabled. He said he would lose like 60 hours of work if we couldn’t recover it. So here is what I did:

    On the SQL instance hosting the content database, I restored the 8/29/11 backup of “WSS_CustomerSite_Content” as WSS_ CustomerSite _Recovery_083111

    On the SharePoint Server,

    Outcome:

    Progress: Import completed.

    Finish Time: 8/31/2011 3:20:44 PM.

    Duration: 00:04:56

    Total Objects: 2002

    Finished with 0 warnings.

    So the process copied the document library into the new website (with 2002 documents) and allowed me to browse to the previous version of the Excel spreadsheet, download it, and email it to the user. Yes, I could have restored the doc library over the top of the production site, but again, that could have disrupted others.

    All this could have been prevented by turning on versioning in the document library. It’s off by default because each version snapshot takes up space and in a large library this could grow pretty significantly. You can keep it under control by specifying how many versions to retain (suggestion: 3 major versions).

    Still, this process is not very elegant. In extreme cases like this, you have to touch SQL Server, Central Admin, and PowerShell just to get a document back. What would make it elegant? A third party backup solution.

    This is what I’ve been telling people for years, because the average customer is not going to want to go through that just to restore a document. Add a BackupExec solution, though, where the agent allows the customer to browse to the version of the document they want, and restore it in place, with no fuss… this is the expected ease of use.

Reference: http://www.akspug.org/Blog/Post/40/How-to-Recover-A-Single-Document-In-SharePoint

Posted in SharePoint 2010, Uncategorized | Leave a comment

Create a cross-site lookup field using PowerShell

Sometimes you need to create a lookup field to look up a list in another site (not the parent site).

This is how  you can do it using PowerShell:

On SharePoint server, run PowerShell as admin:


Add-PsSnapin Microsoft.SharePoint.PowerShell
$targetWeb = Get-SPWeb http://xxxxxx/Subsite1
$sourceWeb = Get-SPWeb http://xxxxxx/Subsite2
$targetList = $targetWeb.Lists.item("The target list name")
$sourceList = $sourceWeb.Lists.item("The source list name")
$NameTheLookupFieldToBeCreated = "Name of the lookup field to be created"
$ColumnInSourceList = "Name of the column in the source list"
### No need to change after this line ###
$targetList.Fields.AddLookup($NameTheLookupFieldToBeCreated, $sourceList.id, "false")
$LookupField = $targetList.Fields[$NameTheLookupFieldToBeCreated]
$LookupField.LookupWebId = $sourceList.ParentWeb.ID
$LookupField.LookupField = $sourceList.Fields[$ColumnInSourceList].InternalName
$LookupField.Update();

Posted in PowerShell, SharePoint 2010 | Leave a comment

Enable Debugging and set custom errors off in SharePoint

The original post is here:

https://blogs.msdn.microsoft.com/voyage/2014/09/02/enable-debugging-and-set-custom-errors-off-in-sharepoint/

Thanks Deepa Lakhani.

To debug a SharePoint solution you need to set following values in web.config

  • 1. Turn on the call stack (CallStack=”true”)
  • 2. Disable custom errors (<customErrors mode=”Off” />)
  • 3. Enable debugging (<compilation debug=”true”>)

Go to \\Inetpub\wwwroot\wss\VirtualDirectories\ Port_Number

This web.config defines configuration settings for a SharePoint Web application.

Now you might still get custom error message on your application page and not the real exception.

This happens because there are web.config at other places too in SharePoint.  So for configuration of layouts directory where an application page resides, you need to change the web.config at following location too.

%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\TEMPLATE\LAYOUTS — The web.config file that defines configuration settings for the /_layouts virtual directory

Additonal Notes from msdn.

Web.config files are contained in the following folders within the file system:

  • \\Inetpub\wwwroot\wss\VirtualDirectories\ Port_Number — The web.config file that defines configuration settings for a SharePoint content Web application.
  • \\Inetpub\wwwroot\wss\VirtualDirectories\ Port_Number_of_Central_Administration — The web.config file that defines configuration settings for the SharePoint Central Administration application.
  • %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\CONFIG — The web.config file and other .config files that together define configuration settings for extending other Web applications.
  • %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\ISAPI — The web.config file that defines configuration settings for the /_vti_bin virtual directory.
  • %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\TEMPLATE\LAYOUTS — The web.config file that defines configuration settings for the /_layouts virtual directory
Posted in Uncategorized | Leave a comment