SharePoint 2013 website tasks
These examples show how to use the .NET Framework CSOM to complete website-related tasks.
Retrieve the properties of a website
Retrieve the title of a SharePoint website.
// Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. ClientContext context = new ClientContext("http://SiteUrl"); // The SharePoint web at the URL. Web web = context.Web; // We want to retrieve the web's properties. context.Load(web); // Execute the query to the server. context.ExecuteQuery(); // Now, the web's properties are available and we could display // web properties, such as title. label1.Text = web.Title;
Retrieve only selected properties of a website
Sometimes, the client is interested only in a few properties of an object. The SharePoint .NET Framework CSOM does not require you to get all properties from the object on a server—you can use anonymous methods, which can be lambda expressions, to specifically request property names. The client library will query only for those properties on the server, and the server will send only those properties to the client. This technique reduces unnecessary data transfer between the client and the server. It is also useful when the user does not have permission to one or more of the other, unused properties on an object. Note that you will need to add a using statement for System.Linq.
// Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. ClientContext context = new ClientContext("http://SiteUrl"); // The SharePoint web at the URL. Web web = context.Web; // We want to retrieve the web's title and description. context.Load(web, w => w.Title, w => w.Description); // Execute the query to server. context.ExecuteQuery(); // Now, only the web's title and description are available. If you // try to print out other properties, the code will throw // an exception because other properties are not available. label1.Text = web.Title; label1.Text = web. Description;
Note |
---|
If you try to access other properties, the code throws an exception because other properties are not available.
|
Write to website's properties
This example shows how to write to the website's properties.
// Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. ClientContext context = new ClientContext("http://SiteUrl"); // The SharePoint web at the URL. Web web = context.Web; web.Title = "New Title"; web.Description = "New Description"; // Note that the web.Update() does not trigger a request to the server // because the client library until ExecuteQuery() is called. web.Update(); // Execute the query to server. context.ExecuteQuery();
Create a new SharePoint website
This example shows how to create a new SharePoint site as a subsite of the current website. Use the WebCreationInformation class to create a new website. You will also need to add using statements for System.Collections.Generic and System.Text.
// Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. ClientContext context = new ClientContext("http://SiteUrl"); WebCreationInformation creation = new WebCreationInformation(); creation.Url = "web1"; creation.Title = "Hello web1"; Web newWeb = context.Web.Webs.Add(creation); // Retrieve the new web information. context.Load(newWeb, w => w.Title); context.ExecuteQuery(); label1.Text = newWeb.Title;
No comments:
Post a Comment