Showing posts with label client object model. Show all posts
Showing posts with label client object model. Show all posts

Tuesday, 2 December 2014

Difference between Load and LoadQuery in client object model


Load method populates the client object directly with what it gets data from the server i.e. a collection object like ListItemCollection etc. but LoadQuery returns the data as a completely new collection in IEnumerable format. Other major difference is that the Collections that you load using the Load() method are eligible for garbage collection only when the client context variable itself goes out of scope where as, in these collections go out of scope at the end of IEnumerable list.

                        You might have noticed that the Client Object Model has two load methods, Load() and LoadQuery(). The Load() method does an in-place loading of the data returned from the server. All of the data returned from the server is stored and tracked inside of the ClientContext object. The ClientContext tracks the object IDs of the items and properties returned from the server. For example if you call the server for the web object but do not return the title on the first call, you could subsequently call the server again for the title. The Title property is now filled in and ready to be used. This convenience does come with some trade-offs. For example, because the objects returned from the server are stored in the ClientContext object, it means that all of the data will only be garbage collected in the Silverlight application when the ClientContext object is cleaned out.


                     The LoadQuery() method returns the results as new objects. These objects are not part of the ClientContext and a result can be easily managed and discarded when not needed any more. Another difference between Load() and LoadQuery() is the type of queries you can write. The Client OM supports two types of queries: LINQ query syntax and method syntax. LINQ query syntax is a more natural declarative query language. This format is one that you are probably more familiar with and what people think of when they write LINQ queries. Listing 8.5 shows an example of a LINQ query to return visible Lists.

Reference:

http://techno-sharepoint.blogspot.in/2012/10/difference-between-load-and-loadquery.html

SharePoint 2010 - Introduction to Client Object Model


SharePoint 2010 provides a new client object model that enables you to create SharePoint solutions that run remotely from the SharePoint server farm. For example, the client object model enables you to consume and manipulate SharePoint data in Windows Forms applications, Windows Presentation Framework application, console applications, Silverlight applications, and ASP.NET Web applications.
 
For working with client object model you need to get two master dlls:
 
·         Microsoft.SharePoint.Client.dll
·         Microsoft.SharePoint.Client.Runtime.dll
 
These dlls you can find under following path from machine on which you have installed SharePoint 2010.
 
Path to get DLL's: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI
 
The SharePoint Foundation 2010 managed client object model consists of two assemblies that contain five namespaces. If you look at the classes available in those namespaces, you see many classes. Primarily classes that have direct counterparts to some familiar classes in the SharePoint Foundation server object model.
 
Following table shows you Client Side classes those are similar to Server Object Model:
 
 
Client Object Model Classes Server Object Model Classes
ClientContext SPContext
Site SPSite
Web SPWeb
List SPList
ListItem SPListItem
Field SPField
  1. Code snippets for How to Load List and List items:
public void LoadList()
        {
            using (SP.ClientContext ctx = new SP.ClientContext("SharePointSiteURL"))
            {
                var web = ctx.Web;

               // Let's only work with the specific List object and save resources
                // by not fetching any other objects
                var list = ctx.Web.Lists.GetByTitle("ListName");
 
                // Load only the list variable and execute the query
                ctx.Load(list);
                ctx.ExecuteQuery();
 
                SP.CamlQuery camlQuery = new SP.CamlQuery();
                 camlQuery.ViewXml =
                            @"<View>
                        <Query>
                                  <Where>
                                <Eq>
                                  <FieldRef Name='Name'/>
                                  <Value Type='Text'>Shailesh</Value>
                                </Eq>
                                  </Where>
                        </Query>
                              </View>";
                SP.ListItemCollection listItems = list.GetItems(camlQuery);
                ctx.Load(listItems);
                ctx.ExecuteQuery();
 
                //Now you can iterate listitems collection and can get listitems using foreach loop
 
                foreach (SP.ListItem listItem in listItems)
                {
                            //you can get value of list column from listitem as follow:
                            //listitem["ID"]
                        //listitem["Name"]
                }
            }
        }
  2. Code snippets for How to insert  List items to a List:
public void SaveListItem()
        {
            using (SP.ClientContext ctx = new SP.ClientContext("SharePointSiteURL"))
            {
                var web = ctx.Web;
                var list = ctx.Web.Lists.GetByTitle("ListName");
                ctx.Load(list);
                ctx.ExecuteQuery();
                SP.ListItemCreationInformation itemCreateInfo = new SP.ListItemCreationInformation();
                SP.ListItem listItem = list.AddItem(itemCreateInfo);
                listItem["Name"] = "Shailesh";
                listItem["Surname"] = "Soni";
                listItem.Update();
               ctx.ExecuteQuery();
            }
        }
  3. Code snippets for update List items in List:
Public  void UpdateListItem()
        {
            using (SP.ClientContext ctx = new SP.ClientContext("SharePointSiteURL"))
            {
                var web = ctx.Web;

               // Let's only work with the specific List object and save resources
                // by not fetching any other objects
                var list = ctx.Web.Lists.GetByTitle("ListName");
 
                // Load only the list variable and execute the query
                ctx.Load(list);
                ctx.ExecuteQuery();
 
                SP.CamlQuery camlQuery = new SP.CamlQuery();
                 camlQuery.ViewXml =
                            @"<View>
                        <Query>
                                  <Where>
                                <Eq>
                                  <FieldRef Name='Name'/>
                                  <Value Type='Text'>Shailesh</Value>
                                </Eq>
                                  </Where>
                        </Query>
                              </View>";
                SP.ListItemCollection listItems = list.GetItems(camlQuery);
                ctx.Load(listItems);
                ctx.ExecuteQuery();
 
                //Now you can iterate listitems collection and can get listitems using foreach loop
 
                foreach (SP.ListItem listItem in listItems)
                {
                             listitem["Address"] = “blah blah blah”;
                            listitem.Update();
                }
                ctx.ExecuteQuery();
            }
        }
  4. Code snippets for delete List items in List:
Public  void DeleteListItem()
        {
            using (SP.ClientContext ctx = new SP.ClientContext("SharePointSiteURL"))
            {
                var web = ctx.Web;

               // Let's only work with the specific List object and save resources
                // by not fetching any other objects
                var list = ctx.Web.Lists.GetByTitle("ListName");
 
                // Load only the list variable and execute the query
                ctx.Load(list);
                ctx.ExecuteQuery();
 
                SP.CamlQuery camlQuery = new SP.CamlQuery();
                 camlQuery.ViewXml =
                            @"<View>
                        <Query>
                                  <Where>
                                <Eq>
                                  <FieldRef Name='Name'/>
                                  <Value Type='Text'>Shailesh</Value>
                                </Eq>
                                  </Where>
                        </Query>
                              </View>";
                SP.ListItemCollection listItems = list.GetItems(camlQuery);
                ctx.Load(listItems);
                ctx.ExecuteQuery();
 
                //Now you can iterate listitems collection and can get listitems using foreach loop
                foreach (SP.ListItem listItem in listItems)
                            listItem.DeleteObject();
                ctx.ExecuteQuery();
            }
        }
 Conclusion: The above code snippets and detail description will help you to understand Client Object Model to begin with SharePoint 2010.
Reference:

http://weblogs.asp.net/shailesh/sharepoint-2010-introduction-to-client-object-model

Saturday, 15 March 2014

The property or field 'Title' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

The property or field 'Title' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

Fix:

 Usually we get this error when we try to use a property before calling ExecuteQuery();
I find a similar thread here you must call executeQuery and use the property.

Reference:

http://social.msdn.microsoft.com/Forums/en-US/f0b4955b-56a2-4af4-b55c-f894b7bf2baa/the-property-or-field-has-not-been-initialized-it-has-not-been-requested-or-the-request-has-not?forum=sharepointdevelopmentlegacy

http://social.technet.microsoft.com/Forums/sharepoint/en-US/245bae70-0fd7-4d17-9f40-5489db537d65/exception-in-clientcontext-class-while-try-to-get-sharepoint-object?forum=sharepointdevelopmentprevious