Linq to SharePoint and RunWithElevatedPrivileges

Today I've tried to use Linq to SharePoint inside a RunWithElevatedPriviles anonymous delegate.

I haven't been lucky, and the reason is that the creation of our SharePoint context creates an instance of the Microsoft.SharePoint.Linq.Provider.SPServerDataConnection class, witch uses the SPContext.Current, if it is not null. From my previous article SharePoint SPEventReceiver, RunWithElevatedPrivileges and Access Denied error about RunWithElevatedPrivileges, I understood immediatly that it was because of some SPSite or SPWeb istantiated outsite the delegate.

In the post LINQ to SharePoint and RunWithElevatedPrivileges is a detailed article explaining what to do in detail to solve the problem:

  • Save the HttpContext.Current and if SPContext.Current != null, set HttpContext.Current to null;
  • Inside the RunWithElevatedPrivileges delegate, create new instances of SPSite and SPWeb, as usual;
  • Create a new (dummy) HttpRequest, a related HttpContext and call the SPControl.SetContextWeb method;
  • Finally, if SPContext.Current != null, reset the HttpContext to its initial value.

SharePoint 2010 and PowerShell guide for feature installation

Following my previous SharePoint 2010 and PowerShell guide for wsp deployment, here is a list of common PowerShell commands to be used with SharePoint 2010 when installing features:

  • Install-SPFeature FeatureName
  • Enable-SPFeature FeatureName -Url http://mywebapp
  • Disable-SPFeature FeatureName -Url http://mywebapp
  • Uninstall-SPFeature FeatureName

In the article Installing or Uninstalling Features there is a useful reference page.

SQL Server: How enable database mail

To enable database mail, from Sql Server Management Studio execute the following query:

EXEC sp_configure 'show advanced options', 1

EXEC sp_configure 'Database Mail XPs', 1

Then from the Management node, right-click Database Mail, and select Configure Database Mail.

Here you can add profiles, and for each profile multiple email accounts.

Remember: Database Mail requires the Sql Server Agent service to run, else messages will accumulate in a query in the Msdb database.

SharePoint: how make your wsp deployable in a single web application

In SharePoint, I've came across the problem to have wsp deployable to all web applications and not to a single one.

I've read that if the wsp contains code, this shoul not happen, but this is not true for me (or better: I have to understand it yet).

I've also found an explanation saying that if your wsp needs to apply web.config modifications, then you can choose the web-applications where deploy it.

So the trick I've found is: add an empty web-part to the wsp (even without no features activating it), so that during wsp deployment the WebPart is added in the web.config safe controls. In this way, the web part is deplyable in a single web application.

ActiveDirectory query examples

If you need to query the ActiveDirectory, you have two ways:

PASSED Exam 70-521 - Upgrade: Transition Your MCPD .NET Framework 3.5 Windows Developer Skills to MCPD .NET 4 Windows Applications Developer

On August, 2nd 2011 I've passed the following Microsoft exam: Exam 70-521 - Upgrade: Transition Your MCPD .NET Framework 3.5 Windows Developer Skills to MCPD .NET 4 Windows Applications Developer.

So, I got the following certifications:

I've also updated the copy of the Microsoft official transcript.

Linq to SharePoint 2010: how map the Created, Author, Modified and Editor fields

The native Linq to SharePoint included in SharePoint 2010 doesn't map the Created, Author, Modified and Editor fields.

But it is possible to extend the entities generated by SPMetal making your partial classes (or better: your Item partial class) implement the ICustomMapping interface and map these fields, as described in the post LINQ To SharePoint: Working with Created, CreatedBy, Modified and ModifiedBy.

SharePoint 2010: executing a script after the load of the page

In SharePoint 2010, I've been in the need to open a popup page after a post-back in a web-part.

I tried the OpenPopUpPage javascript function, but it didn't work, because the javascript files are loaded at the end of the page.

I debugged the javascript in the page, and found the ExecuteOrDelayUntilScriptLoaded method, that you can use in this way:

ExecuteOrDelayUntilScriptLoaded (myFunction, "core.js");


ExecuteOrDelayUntilScriptLoaded (function() { insert my code here }, "core.js");


Then I googled a bit about it, and found the excellent tutorial SharePoint Scripts On Demand (SP.SOD) about this function and SOD in general (SOD stands for SharePoint Scripts On Demand).