Showing posts with label Client Object Model. Show all posts
Showing posts with label Client Object Model. Show all posts

Monday, November 18, 2013

Client Object Model

Client Object Model is a  feature of SharePoint 2010. It provides programming in a SharePoint site using .Net Managed Code or JavaScript.

The Client Object Model provides almost all the programming features of the Server Object Model plus advantages on deployment. The Client OM (Client Object Model) is being used as the core programming aid for SharePoint 2010 and thus widely used in the market.

Advantages

  1. Less Deployment Hassles: Using the Client OM, you do not need to install the components required by Server Object Model. Thus the Client OM provides significant conveniences for the end user.
  2. Language Flexibility: We can use the following languages to work with the Client OM:

    a. Microsoft .Net
    b. Silverlight
    c. ECMA Script (JavaScript / Jscript)
     
  3. Query Speed Optimizations: In the Client OM, reduced network traffic is attained using Query Optimizations. Thus the user will feel reduced round trips and other advantages like paged results etc.
  4. everywhere using COM we can access the SharePoint data from the client side browser itself with Silverlight and ECMAScript applications.
  5. Web parts with very rich user interface can be developed with the help of Silverlight and jQuery. For example, we can create web parts like video and image galleries with beautiful animations where the videos and the images can be stored in the SharePoint libraries and retrieved using COM.
  6. COM along with JavaScript or jQuery can be implemented with just a Content Editor Web part within the browser or from the SharePoint designer without opening the Visual Studio. You can read about it here on how to do that.
  7. When we save your site as a template. The application developed with ECMAScript or Silverlight application implemented with Client Object Model would come along with the template which would be very helpful during migrations though we need to take care of any hard coded values.
  8. No IISREST is required while deploying a Silverlight or an ECMAScript application implemented with COM.
  9. We can have SharePoint accessed from the client desktop using Windows forms or WPF applications implemented with COM. We can develop desktop gadget kind of applications which would be displaying the new announcements added to the “Announcements” list.
  10. No SharePoint installation is required in the development machine. Only the dll’s are required if you are going to develop Silverlight applications.
Disadvantages

1.      We cannot elevate the privilege or Impersonate in COM as in Server Object Model. That is, we cannot use the RunWithElevatedPrivilege kind of a delegate. Therefore, the results retrieved using COM will always be security trimmed by default.
2.      The range of classes available to access SharePoint data is very limited in COM. For example, we do not have Classes for accessing User Profiles. We need to go for SharePoint web services.
3.      We cannot access the objects of another site collection in COM while implementing in Silverlight or in ECMAScript. We will get “The security validation for this page is invalid.” if we try to do so. So, we cannot develop applications to read the data from another site collection.
4.      Silverlight web parts fail to load by giving an error message saying “Could not download the Silverlight application or the Silverlight Plugin did not load. To re-configure the Web Part or to provide a different Silverlight application (.xap), open the tool pane and then click Configure.” when it takes more than 5 seconds to load.
5.      We cannot retrieve recurring events from a SharePoint calendar list in Client Object Model.

    Friday, November 15, 2013

    Client Object Model vs Server Object Model in SharePoint 2010

    Client Object Model vs Server Object Model in SharePoint 2010 ?

    Server Object Model
    Client Object Model
    Server Object Model is like a Production Server Environment to access the data where shapoint server installed on the machine .
    Client Object Model  can access the data from the system where sharepoint server is not  installed on the machine via Web Services , API , JavaScript etc 
    Most extensive set of options to customize SharePoint 2010. Any Components consuming server-side object model needs to be deployed on the server
    Client OM APIs are wrappers around a custom Web service that calls the server-side object model.
    Slower Performance than Client OM and REST API(s)
    Better Performance than server-side OM
    Synchronous
    Asynchronous
    Server Object Model  is slower than client object model.  But to access Farm object we can use Server object model only
    Client object model gives result in fast manner, but there is a limitation. The limitation is, we can not access Farm Object using Client object model.

    Monday, April 29, 2013

    Sharepoint 2010 Object model Tutorial


    Sharepoint 2010 Object model Tutorial


     Microsoft SharePoint Foundation offers a highly structured server-side object model that makes it easy to access objects that represent the various aspects of a SharePoint Web site. From higher-level objects, you can drill down through the object hierarchy to obtain the object that contains the members you need to use in your code.

    Server Architecture





    1. The SPFarm object is the highest object within the SharePoint Foundation object model hierarchy. The Servers property gets a collection representing all the servers in the deployment, and the Services property gets a collection representing all the services.

    2. Each SPServer object represents a physical server computer. The ServiceInstances property provides access to the set of individual service instances that run on the individual computer.

    3. Each SPService object represents a logical service installed in the server farm. Derived types of the SPService class include, for example, objects for Windows services, such as the timer service, search, the database service, etc. and also objects for Web services, such as the basic content publishing Web service which supports the Web applications.

    4. An SPWebService object provides access to configuration settings for a specific logical service or application. The WebApplications property gets the collection of Web applications that run the service.

    5. If the service implements the Service Application Framework of SharePoint Foundation, then it can be split into multiple configured farm-scoped instantiations (CFSIs). Each of these provides the functionality of the service but each has its own individual permission and provisioning settings.

    6. Each instance of a service, or a CFSI, that is running on a specific server is represented by an SPServiceInstance object.


    7. An SPDatabaseServiceInstance object represents a single instance of a database service running on the database server computer. The SPDatabaseServiceInstanceclass derives from the SPServiceInstance class and thus inherits the Service property, which provides access to the service or application that the instance implements. The Databases property gets the collection of content databases used in the service.

    8. Each SPWebApplication object represents a Web application hosted in an Internet Information Services (IIS) Web site. The SPWebApplication object provides access to credentials and other farm-wide application settings. The Sites property gets the collection of site collections within the Web application, and theContentDatabases property gets the collection of content databases used in the Web application.

    9. An SPContentDatabase object inherits from the SPDatabase class and represents a database that contains user data for a Web application. The Sites property gets the collection of site collections for which the content database stores data, and the WebApplication property gets the parent Web application.

    10. An SPSiteCollection object represents the collection of site collections within the Web application.



    Server Object Model 


    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 referred 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 highest level 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["mySite"];

    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["mySite"]);
    {
    }

    You can also use the Openweb() as below

    using (SPWeb oWebSite = mySiteCollection.OpenWeb(“mySite”))
    {
    }

    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 

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;

        class Program
        {
            static void Main(string[] args)
            {
                //To return the collection of site collections in a SharePoint Web application -
                SPSite oSiteCollection = new SPSite("http://staff-1/");
                 foreach (SPWeb sw in oSiteCollection.AllWebs)
    {
        Console.WriteLine(sw.Url);


    }


            }
        }


    Note:
    [if any running problem change   the visual studio solution to 64 bit in properties  ]

    Note : To run the 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) + “<BR>”;
    }
    }
    }
    }

    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) + “<BR>”;

    SPListCollection collList = subSite.Lists;

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

    }subSite.Close();
    }
    }}
      



    Client Object Model vs Server Object Model in SharePoint 2010 ?


    using Client Object Model we can access the data from the system where sharepoint server is not installed on the machine via Web Services , API , JavaScript etc

    Where as In Server Object Model is like a Production Server Environment to access the data where shapoint server installed on the machine .