Accessing Site/Web/List/List Item – Server/Client Object Model

There are two approaches we can choose when creating SPWeb objects depending on the context the code runs in.

(1) In an ASP.NET application, information about the current application, each user session, the current HTTP request, the requested page, etc., is stored in the HTTP context object. The first approach takes advantage of the HTTP context object. Within a SharePoint HTTP context we can use the Microsoft.SharePoint. SPContext class to obtain information for the current application, such as the current site collection, the current SharePoint site or list object if the application that needs to access the object is in the same context.

Note that SPSite and SPWeb objects returned by SPContext.Site, SPContext.Current.Site, SPContext.Web, and SPContext .Current.Web
do not need to be disposed as SPContext objects are managed by the SharePoint framework.

For example,

SPWeb web=SPContext.Current.Web

Two ways to open a WEB

either:
SPSite sitecollection = SPContext.Current.Site;

Using (SPWeb website = siteCollection.OpenWeb(“http://…….”))

{
}

or:

SPSite sitecollection = SPContext.Current.Site;

Using (SPWeb website = siteCollection.AllWebs[“MyWebSite”])

{
}

(2) Use SPSite constructor to create a reference to the site collection, and then a reference to the web etc.

using (SPSite site = new SPSite(“….”))

{
Using(SPWeb web-site.AllWebs[])
{

 

}
}

Add a new list tiem to a list (SERVER MODEL)

SPSite siteCollection = SPContext.Current.Site
using (SPWeb website = siteCollection.RootWeb)
{

SPListItemCollection listItems = website.Lists[“SomeListName”].Items;
SPListItem item = listitems.add();
item[“Title”]=”dasdfasdf”;

item[“Budget”]=”dasdfasdf”;

item[“Usage”]=”dasdfasdf”;

item.Update();

}

Search an list item and update it (SERVER MODEL)

(1) inefficient – because it loops through every single list item in the list

SPSite siteCollection = SPContext.Current.Site
using (SPWeb website = siteCollection.RootWeb)
{

SPListItemCollection listItems = website.Lists[“SomeListName”].Items;
foreach(SPItem item in listItems)
{
if (“Target”==item[“Usage”].ToString())
{
item[“Budget”]=”dasdfasdf”;

item[“Usage”]=”dasdfasdf”;

item.Update();
}
}

}

(2) efficient – because it uses SPQuery/CAML

SPSite siteCollection = SPContext.Current.Site
using (SPWeb website = siteCollection.RootWeb)
{

SPList list = website.Lists[“Listname”];
SPQuery query= new SPQuery();
query.Query = “<Where><Eq><FieldRef name=’Usage’/>

<Value Type=’Text’>

Target</Value></Eq></Where>”;
SPListItemCollection listItems = list.GetItems(query);

SPItem item – listItem[0];

item[“Budget”]=”dasdfasdf”;

item[“Usage”]=”dasdfasdf”;

item.Update();

}

Working with list item (Client MODEL)

Create new list item – client

string siteURL=”http://localhost“;

ClientContext clientContext = new ClientContext(siteUrl);

List list=clientContext.Web.Lists.getByTitle(“somelistname”);
ListItemCreationInformation itemCreateinfo = new ListItemCreationInformation();
ListItem item = list.AddItem(itemCreateInfo);

item[“title”]=”sdfasdfasdf”;
item[“field1″]=”sdfasdfasdf”;

item[“field2″]=”sdfasdfasdf”;

item.Update();

update list item – client

string siteURL=”http://localhost“;

ClientContext clientContext = new ClientContext(siteUrl);

List list = clientContext.Web.Lists.getByTitle(“somelistname”);
CamlQuery camlQuery= new CamlQuery();
camlQuery.ViewXML = “<Where><Eq><FieldRef name=’Usage’/>

<Value Type=’Text’>

Target</Value></Eq></Where>”;
ListItemCollection collListItem = list.Getitems(camlQuery);
clientContext.Load(collListItem);

clientContext.ExecuteQuery();

listItem item – CollListItem[0];
item[“title”]=”ccsadasdfasd”

item[“Budget”]=”dasdfasdf”;

item[“Usage”]=”dasdfasdf”;

item.Update();
clientContext.ExecuteQuery();

}

delete list item – client

string siteURL=”http://localhost“;

ClientContext clientContext = new ClientContext(siteUrl);

List list = clientContext.Web.Lists.getByTitle(“somelistname”);
CamlQuery camlQuery= new CamlQuery();
camlQuery.ViewXML = “<Where><Eq><FieldRef name=’Usage’/>

<Value Type=’Text’>

Target</Value></Eq></Where>”;
ListItemCollection collListItem = list.Getitems(camlQuery);
clientContext.Load(collListItem);

clientContext.ExecuteQuery();

listItem item – CollListItem[0];
item.DeleteObject();
clientContext.ExecuteQuery();

}

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: