Pages

Tuesday, October 18, 2011

CAML to Query User field

Sharepoint as a database is more of a Flat structure database. Querying is often times the most time consuming operation in especially when running against millions of List data. Microsoft has done a lot to increase the query time, still there are some limitations in sharepoint. Hence it is necessary to optimize the CAML as much as possible when querying against large Lists or document Libraries.

Often times I get asked by team members about writing CAML against Lookup Field and User Field. I decided to write it down here as a reference for everybody who wants to use CAML to query Sharepoint user field for me as well because every time some one asks me I tend to forget and needs to go back to code and give them the query. So I thought this is a right form so every body can be benefited.


Code:

SPQuery query = new SPQuery();
query.Query = string.Format("<Where><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Int'>{0}</Value></Eq></Where>", SPContext.Current.Web.CurrentUser.ID);
SPListItemCollection itemCollection = oList.GetItems(query);



I am using the sharepoint user field as sharepoint lookup field and performing the query based on user id. I prefer this method because there is no way this could go wrong as no 2 users can have same ID. This struck me when my team was implementing FBA and we created test user with same name. Interestingly the same query can be used for sharepoint Lookup field as well. If you notice the way Sharepoint stores the Look up field you can the format as

1;#lookupvalue

This same format is used for SPUser field as well. This set up is actually a blessing for us  "Developers"

Monday, October 10, 2011

Sharepoint Web Events and Branding

Sharepoint Branding is one of the important thing in Sharepoint development. Often times there is a requirement to create branding features and feature stapler to achieve required branding. I think feature stapler is definetly a great idea, but in sharepoint 2010 there a new concept that is introduced in solution deployment. In sharepoint 2007 we did not had any sandbox solution. In a nut shell Sharepoint Sandbox solution is a way to deploy solution that can be used only in site collection that is deployed. These sandbox solution doesnot have access to file system. Here comes the problem because it cannot access the file system feature stapler cannot be used to add new features. Another change or improvement in sharepoint 2010 is web templates. Web templates replaces site templates in sharpoint 2007. These web templates are again deployed as Sandbox solution.hence feature stapler cannot be used to attach any branding as most often web templates are used for creating sites with similar look and feel and branding.

After researching quite a bit I felt that there is less documentation on how to achieve branding or change certain functionality in sandbox solution using feature stapler. Having used feature stapler heavily in MOSS 2007 I felt hard to get away with it. I did experiment with some of the events and see other ways to achieve this. I found the Web Events that are introduced in Sharepoint 2010 is a better way to achieve this. In sharepoint 2010 we can use "Site was provisioned" which is an Asynchronous event to achieve some of the branding stuff. I recently created this to set master pages for the sites created using Sandbox Web templates.

Code 

public class SetMasterPageEvent : SPWebEventReceiver
{
       /// <summary>
       /// A site was provisioned.
       /// </summary>
       public override void WebProvisioned(SPWebEventProperties properties)
       {
           base.WebProvisioned(properties);

           using (SPSite site = new SPSite(properties.FullUrl))
           {
               using (SPWeb web = site.OpenWeb())
               {
                   web.MasterUrl = web.Site.RootWeb.MasterUrl;
                   web.CustomMasterUrl = web.Site.RootWeb.CustomMasterUrl;
                   web.AlternateCssUrl = web.Site.RootWeb.AlternateCssUrl;
                   web.SiteLogoUrl = web.Site.RootWeb.SiteLogoUrl;
                   web.Update();
               }
           }
       }


    }


In the example I am setting the master page of the newly created site to the master page of root site. You can register this event by creating a site collection feature if you want the operation to be performed on all the sites that are created under the site collection or a web feature if an operation needs to performed on a specific site.