Impersonation for SharePoint 2010 LINQ to DataContext

Impersonation in SharePoint is not really the hardest to do. However the C# code snippet below was sort of new for me. In order to obtain impersonation for a DataContext, that was something.

Impersonation for the ClientContext … but the LINQ DataContext is constructed in another way… and by default using the current user… regardless the user you’ve set in SPContext.

So… here it is.

 

strUrl = SPContext.Current.Web.Url;

HttpContext backupContext = HttpContext.Current;
try
{
  
SPUserToken userToken = null;
  userToken =
SPContext.Current.Web.AllUsers[“SHAREPOINT\\system”].UserToken;

  // if there is a SPContext make it is as null so LINQ won’t execute under current context
  
if (SPContext.Current != null)   HttpContext.Current = null;

  SPSecurity.RunWithElevatedPrivileges(delegate() {
    using (SPSite oSite = newSPSite(strUrl, userToken)) {
      using (SPWeb oWeb = oSite.OpenWeb()) {
                               // creating a new HttpContext under elevated permission and
             // setting it as current control context web,
             // because of this the code will be running under elevated permission.

        HttpRequest httpRequest = newHttpRequest(“”, oWeb.Url, “”);
        HttpContext.Current = newHttpContext(httpRequest, new HttpResponse(new System.IO.StringWriter()));
        
SPControl.SetContextWeb(HttpContext.Current, oWeb);

        using (SomeDataContext dc = SomeDataContext(oWeb.Url)) 
        
{
          var comments = from c in dc.SomeDataContext
                         where c.Var1 == someVariable 
                        
select c;
                    }
      }
    }
  });

}

catch (Exception ex) {}
finally {
   // make sure that you are setting the actual SPContext back
   //
after your operation and make SharePoint happy J

   if (SPContext.Current != nullHttpContext.Current = backupContext;
}

Advertisements

Search SharePoint 2010 Managed Metadata Terms with an Ampersand

Already struggling with the poor functionality of the web service interface of the Taxonomy web service of SharePoint 2010, I stumbled upon another challenge.

I was building a MS CRM 2011 Plugin that populates the SharePoint Term Store using the Taxonomy web service (http://<sharepointurl>/_vti_bin/TaxonomyClientService.asmx).
The first problem was the poor functionality of the web service like being unable to add Labels to Terms.
This cannot be fixed worked around.

Then I had to query SharePoint for a Term that contained an ampersand.
Adding the Term was not a problem when the XML was escaped properly.

string _newterm = “V&D”;

_newterm = _newterm.Replace(“&”, “&amp;”).Replace(“<“, “&lt;”).Replace(“>”, “&gt;”).Replace(“\””, “&quot;”).Replace(“‘”, “&apos;”);

 

But when the SharePoint is queried for a certain Term… it works slightly different.

The TermStore can be queried for example using “GetTermsByLable()”

 

string TermXml = SPTaxService.GetTermsByLabel(TermLabel,

1033,


StringMatchOption.ExactMatch,

10,

NewTermXml,


false);

 

When you send use the TaxonomyClientService.asmx, the service will not find your term, even though when you created it you used the exact same string.

The reason is that an & will be stored by SharePoint as \uFF06

So when you wanna search for a Term, make sure you replace the ampersand… like…

 

string XmlTerm = “<newTerms><newTerm label=\”” + _newterm.Replace(“&”, “\uFF06”) + “\” clientId=\”1\” parentTermId=\”” + Guid.Empty + “\”></newTerm></newTerms>”);

 

The weird thing is, that the VS2010 Debugger still will show you the &amp;
The result from SharePoint is the right one…

So …replacing the & with &amp; does not work..!

Searching SharePoint from Windows7 Windows Explorer

In Windows7 you can create custom search connectors to OpenSearch search engines.

And YES… SharePoint 2007 en 2010 publish OpenSearch standard search engines.

It is really simple to create a connector to SharePoint.

  1. Open Notepad and copy in the XML below
  2. Replace the areas in red with your SharePoint Search Center information.  Be sure to only replace just the part of the URL that gets to your Search Center:  e.g.  http://sharepoint.company.com/SearchCenter  
  3. Change the scope if you want:  e.g.  &amp;s=Intranet  or  &amp;s=All%20Sites  or even  &amp;s=People
  4. Save the file to your Desktop as:    SearchMySharePointSite.osdx
  5. Double click the file to install the Search Connector.  (it will install to your “Searches” directory  e.g.   c:\Users\<username>\Searches

And if you don´t have enough connectors… here is the rest of them

Amazon, DeviantArt, eBay, Flickr, Google Blogs, Google News, Live Search, MSN Search, Redmond Pie, Wikipedia, Yahoo and YouTube


————————————————————————————

<?xml version=”1.0″ encoding=”UTF-8″?>
<OpenSearchDescription xmlns=”http://a9.com/-/spec/opensearch/1.1/” xmlns:ms-ose=”http://schemas.microsoft.com/opensearchext/2009/”>
<ShortName>SharePoint Search</ShortName>
<Description>Search the SharePoint site.</Description>
<Url type=”application/rss+xml” template=”http://sharepoint.company.com/searchcenter/_layouts/srchrss.aspx?k={searchTerms}&amp;s=All%20Sites“/>
<Url type=”text/html” template=”http://sharepoint.company.com/searchcenter/Pages/Results.aspx?k={searchTerms}&amp;s=All%20Sites“/>
<ms-ose:ResultsProcessing format=”application/rss+xml”>
<ms-ose:LinkIsFilePath>-1</ms-ose:LinkIsFilePath>
</ms-ose:ResultsProcessing>
</OpenSearchDescription>

Information Worker VHD 2010

There’s a new Information worker are ready to be downloaded. You can find them here.

BUT…. can we I download them all the parts at once.

YEAH…. you can. Use the Akamai download manager.

The download is about 18 GB big.

This is the content :

Virtual machine “A” contains the following pre-configured software:

  1. Windows Server 2008 R2 Standard Evaluation Edition x64, running as an Active Directory Domain Controller for the “CONTOSO.COM” domain with DNS and WINS
  2. Microsoft SQL Server 2008 R2 Enterprise Edition with Analysis, Notification, and Reporting Services
  3. Microsoft Office Communication Server 2007 R2
  4. Microsoft Visual Studio 2010
  5. Microsoft SharePoint Server 2010 Enterprise Edition
  6. Microsoft Office Web Applications
  7. Microsoft FAST Search for SharePoint 2010
  8. Microsoft Project Server 2010
  9. Microsoft Office Professional Plus 2010
  10. Microsoft Visio 2010
  11. Microsoft Project 2010
  12. Microsoft Office Communicator 2007 R2

Virtual machine “B” contains the following pre-configured software:

  1. Windows Server 2008 R2 Standard Evaluation Edition x64, joined to the “CONTOSO.COM” domain
  2. Microsoft Exchange Server 2010

Active directory has been preconfigured over 200 “demo” users with metadata in an organizational structure. All of these user profiles have been imported and indexed for search within SharePoint Server 2010.
SharePoint Server 2010 has been configured in a “Complete” farm using the default SQL Server 2008 R2 instance. A default site has been created using the Team Site template at http://intranet.contoso.com/ and a FAST Search Center at http://intranet.contoso.com/search/.
As shipped virtual machine “A” requires image “B” for email. Running image “B” is optional.