Ideas for Free

Ideas for Free

only freedom gives SharePoint real power to grow

Ideas for Free RSS Feed
 

Archive for April, 2009

Howto: Use lambda expression in SharePoint Object model

English

Lambda expression has been introduced since .NET framework 3.5, it is an anonymous function that can contain statement and expression. For more understanding on lambda expression you can read directly in MSDN page here. I will assume that you have read the topic and you can remember the lambda simply as:

(input parameters ) => operation

We will start with very basic operation of using lambda expression in SOM, and I hope you’ll find your path for more complex one. (note some code may not be efficient, for the clarity purpose)

A.  Start with OfType<TResult>() function to get IEnumerable that implement query pattern.

Example:

Originally to browse to all Site collection in WebApplication we will write,

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

SPSiteCollection allSPSite = webApplication.Sites;

foreach(SPSite spSite in allSPSite)

{

      // operation in the SPSite

}

the equivalent for our lambda starter is,

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

var allSPSite = webApplication.Sites.OfType<SPSite>();

foreach(SPSite spSite in allSPSite)

{

      // operation in the SPSite

}

B. Use lambda expression in the IEnumerable

Example :

You need to list all Site collection which uses STS site template.

Originally you will write,

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

SPSiteCollection allSPSite = webApplication.Sites;

foreach(SPSite spSite in allSPSite)

{

      if(spSite.RootWeb.WebTemplate.Equals(“STS”,StringComparison.InvariantCultureIgnoreCase))

     {

           // Do operation in selected site

     }

}

the equivalent using lambda is,

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

var allSPSite = webApplication.Sites.OfType<SPSite>().Where(s => s.RootWeb.WebTemplate.Equals("STS", StringComparison.InvariantCultureIgnoreCase));

foreach(SPSite spSite in allSPSite)

{

      // operation in the SPSite

}

Ok, now I believe you will get the idea of how to use lambda expression in SOM collection. In next posting, I’ll cover directly to the sample usage of the lambda.

Critics to SharePoint resource deployment “stsadm –o copyappbincontent”

English

When you start to work with localization in SharePoint, I believe that you may end with one of Mikhail Dikov proposal here or here .

Partially I agree with his proposal, to leverage the resource deployment through the UI. However, his strategy may fail when you apply to server farm architecture with multiple Web Front End (WFE).

You have to go to every WFE, and activate/deactivate the feature before you can get it deployed in all WFE. The same concept as standard SharePoint’s stsadm command,

stsadm –o copyappbincontent

That command, instruct local SharePoint server to copy resources, sitemap etc. to web all application folder, except the Central Admin webapps.

And still we have big problem, that we have to run that command in every WFE.

I can say this is a big problem, because it against the concept of centralized deployment of WSP. Imagine, if you’re system administrator who wants to have WSP to automate distribution of artifact – and now you are facing a fact that since the WSP contains resources; you don’t have the centralized deployment concept anymore.
After doing

stsadm –o addsolution

stsadm –o deploysolution

in one of WFE – then you have to go through all WFE to execute

stsadm –o copyappbincontent

to deploy resources to webapps.

I know that some system administrator / production team, so we have to solve this problem. May be change it to stsadm –o copyfarmappbincontent , to perform appbincontent deployment in farm architecture. 

Computers Business Directory - BTS Local