Thursday 22 December 2011


SharePoint 2010 Object Model

Microsoft has replaced the "12 hive" structure that we had in SharePoint 2007 with "14 Hive" structure in 2010.

It has apparently added four new folders to its hive.

The Folders are :
* Policy
* UserCode
* WebClients
* WebServices

See the Details at : 14 hive Directory structure



Some of the folders in 14 hive are :

Program Files\Common files\Microsoft Shared\Web Server Extensions\14 -
This directory is the installation directory for core SharePoint Server files.

Program Files\Common files\Microsoft Shared\Web Server Extensions\14\ADMISAPI -
This directory contains the soap services for Central Administration. If this directory is altered, remote site creation and other methods exposed in the service will not function correctly.

Program Files\Common files\Microsoft Shared\Web Server Extensions\14\CONFIG -
This directory contains files used to extend IIS Web sites with SharePoint Server. If this directory or its contents are altered, Web application provisioning will not function correctly.

Program Files\Common files\Microsoft Shared\Web Server Extensions\14\LOGS -
This directory contains setup and run-time tracing logs.

Other newly added folders are :

Program Files\Common files\Microsoft Shared\Web Server Extensions\Policy -


Program Files\Common files\Microsoft Shared\Web Server Extensions\UserCode -
This directory contains files used to support your sandboxed solutions.

Program Files\Common files\Microsoft Shared\Web Server Extensions\WebClients -
This directory contains files related to the new Client Object Model.

Program Files\Common files\Microsoft Shared\Web Server Extensions\WebServices -
This directory contains new wcf or .svc related files.

Note : You should rewrite and recompile any code that refers to files and resources in "12" Hive structure.For example, if you have redeployed all of your files into the "14" folder and emptied your "12" folder, any references to files under the "12" folder will not work.

In Sharepoint 2010 This is a step-by-step tutorial to learn using sharepoint 2010′s Server and client object model.-


Client Object Model is introduced in SharePoint 2010 which helps to developers to make things easier to develop the client side applications for SharePoint 2010. Client Object Model can run on the client machines (Where SharePoint is not installed) and communicate with the SharePoint server remotely. This is the great advantage for the SharePoint devs as they don't have to install SharePoint for development any more.


When we want to start coding with the Client OM there is a little difference between the server-side object model programming, as the class names are very similar to those used in classes so far (not a clear rule, but in general have been removed the letters “SP” from the names of classes in the client-side API). Some differences are visible in this table.



Server Object ModelClient Object Model
SPContextClientContext
SPWebWeb
SPListList
SPListCollectionListCollection
SPListItemListItem
SPFieldField

Assemblies to use in .NET client object model.
Custom .NET applications will need to add references to two assemblies in order to use the client object model. These two assemblies are found on the SharePoint server: c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI:
  • Microsoft.SharePoint.Client.dll (contains the client object model)
  • Microsoft.SharePoint.Client.Runtime.dll (handles all communication between the client and SharePoint server)
And the using statement will look like this:
  • using Microsoft.SharePoint.Client;
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Net;  
using System.Windows;  
using System.Windows.Controls;  
using System.Windows.Documents;  
using System.Windows.Input;  
using System.Windows.Media;  
using System.Windows.Media.Animation;  
using System.Windows.Shapes;  
using Microsoft.SharePoint.Client;  
  
namespace SP2010Silverlight_HelloWorld{  
    public partial class MainPage : UserControl  
    {  
        public MainPage()  
        {  
            InitializeComponent();  
        }  
  
        private ClientContext context = null;  
        private Web web = null;  
        private delegate void UpdateUIMethod();  
  
        private void btnLoadSite_Click(object sender, RoutedEventArgs e)  
        {  
            context = ClientContext.Current;  
            web = context.Web;  
            context.Load(web, w => w.Title, w => w.Description, w => w.ServerRelativeUrl);  
            context.ExecuteQueryAsync(OnSiteLoadSuccess, OnSiteLoadFailure);  
        }  
  
        private void OnSiteLoadSuccess(object sender, ClientRequestSucceededEventArgs e)  
        {  
            UpdateUIMethod updateUI = LoadSiteData;  
            this.Dispatcher.BeginInvoke(updateUI);  
        }  
        private void OnSiteLoadFailure(object sender, ClientRequestFailedEventArgs e)  
        {  
            MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);  
        }  
  
        private void LoadSiteData()  
        {  
            canvasLabels.Visibility = System.Windows.Visibility.Visible;  
            label2.Content = web.Title;  
            label4.Content = web.ServerRelativeUrl;  
            label6.Content = web.Description;  
        }  
    }  
}  



Server Object Model

Here we will look at how to use SharePoint API’s, LINQ, REST and SharePoint web service to extract data from sharepoint server.

Lets Start with using the API’s in Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

Firstly, to work with SharePoint 2010 components, your code must first establish the site context or site collection context for requests that are made to the server.

Please Note : In SharePoint, the SPsite object also refered to as Site is actually a “Site Collection” object, not a website
and the SPweb object also refered to as “web” is a single site in the site collection.(It can be a top-level site collection site).
also, object of type SPWebApplication is a big boss object which has reference to the web applictaion that contains the site collection.

To get the reference to site context in your code use the recommended Microsoft.SharePoint.SPContext class and its members.

Lets look at how it is used

To get a reference to the site collection -

SPSite oSiteCollection = SPContext.Current.Site;

To get a reference to the current web site or web in the site collection -

SPWeb oWebSite = SPContext.Current.Web;

or

SPWeb oWebSite = SPControl.GetContextWeb(Context);

Note : if your are using Microsoft.SharePoint.SPContext class, you should not dispose any SPSite or SPWeb object obtained
by any of the above methods. The SharePoint Foundation runtime will dispose of them after page completion.

To get a reference to all the webs or sites in a site collection -

SPWeb oWebSite = SPContext.Current.Site.AllWebs["mySite1"];

oWebSite.Dispose();

Note : You should explicitly dispose of references to objects that are obtained through the AllWebs() or Openweb() property. You can also use using clause
like below to avoid calling the dispose off method and let sharepoint do this for you.

using can be something like

using (SPWeb oWebSite = SPContext.Current.Site.AllWebs["mySite1"]);
{

}

You can also use the Openweb() as below

using (SPWeb oWebSite = mySiteCollection.OpenWeb(“mySite1″))
{

}

Lets look at some other components of the SharePoint farm that you can get using SPContext

To get a reference to the current top-level server farm object -

SPFarm myFarm = SPContext.Current.Site.WebApplication.Farm;

To get a reference to the site collection database -

SPSite oSiteCollection = SPContext.Current.Site.CurrentDatabase

Lets look at some of the general code snippets

To return the collection of site collections in a SharePoint Web application -

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

using (SPSiteCollection siteCollections = webApplication.Sites)
{

foreach (SPSite siteCollection in siteCollections)
{
Label1.Text += siteCollection.Url + “
”;

siteCollection.Close();
}

}

Note : To runthe above code reference the Microsoft.SharePoint.Administration.SPWebApplication assembly in your code.

To return the collection of The all the Webs or sites within a site collection, including the top-level site and all subsites.

SPSite oSiteCollection = SPContext.Current.Site;
using(SPWebCollection collWebsite = oSiteCollection.AllWebs);
{

for (int i = 0; i < collWebsite.Count; i++)
{
using (SPWeb oWebsite = collWebsite[i])
{
SPListCollection collList = oWebsite.Lists;

for (int j = 0; j < collList.Count; j++)
{
Label1.Text += SPEncode.HtmlEncode(collWebsite[i].Title) + ” ”
+ SPEncode.HtmlEncode(collList[j].Title) + “
”;
}
}}}

To return the all the subsites and lists of the current site

using (SPWeb oWebSite = mySiteCollection.OpenWeb())
{

using(SPWebCollection subSites = oWebsite.Webs)
{

foreach (SPWeb subSite in subSites)
{
Label1.Text += SPEncode.HtmlEncode(subSite.Title) + “
”;

SPListCollection collList = subSite.Lists;

foreach (SPList oList in collList)
{
Label1.Text += SPEncode.HtmlEncode(oList.Title) + ” ” +
oList.ItemCount.ToString() + “
”;

}subSite.Close();
}
}} 

No comments:

Post a Comment