Client Object Model: Query Approved Items and Add Them to Another List

Played around with client object model today to query for approved items and add them to another list.

string rootSite = “<rootSiteUrl>”;
ClientContext rootContext = new ClientContext(rootSite);

string enSite = “<otherSiteUrl>”;
ClientContext enContext = new ClientContext(enSite);

NetworkCredential credentials = new NetworkCredential(“username”, “password”, “domain”);
rootContext.Credentials = credentials;
enContext.Credentials = credentials;

SP.List syndList = rootContext.Web.Lists.GetByTitle(“<List1Name>”);
SP.List pagesLibrary = enContext.Web.Lists.GetByTitle(“<List2Name>”);

CamlQuery camlQuery = new CamlQuery();
//Approved=0, Rejected=1, Pending=2, Draft=3, Scheduled=4
camlQuery.ViewXml = “<View><Query><Where><Eq><FieldRef Name=’_ModerationStatus’/><Value Type=’ModStat’>0</Value></Eq></Where></Query><RowLimit>10</RowLimit></View>”;

ListItemCollection collListItem = pagesLibrary.GetItems(camlQuery);

enContext.Load(collListItem, items => items.Include
(item => item[“Title”],
item => item[“FileRef”]));

foreach (ListItem oListItem in collListItem)
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = syndList.AddItem(itemCreateInfo);
newItem[“Title”] = oListItem[“Title”];
newItem[“PageURL”] = rootSite+oListItem[“FileRef”];


MSDN: Create a client app

MSDN: Retrieve List Items

MSDN: Create, Update, and Delete List Items


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: