Wednesday, November 18, 2009

SharePoint : Setting List column values

Here is the code snippet where I am setting a List column called "Has Payment" depending on few conditions:

string strSiteCollPath = "your Site Collection path";
using (SPSite siteCollection = new SPSite(strSiteCollPath))
{
using (SPWeb siteWeb = siteCollection.OpenWeb())
{
SPList lstPayment = siteWeb.Lists["XYZ"];
SPList lstInvoiceSummary = siteWeb.Lists["ABC"];

foreach (SPItem item in lstInvoiceSummary.Items)
{
string strDocNumber = item["documentNumber"].ToString();
SPQuery query = new SPQuery();
query.Query = "your SPQuery";
SPListItemCollection coll = lstPayment.GetItems(query);
int count = coll.Count;

if (count > 0)
{
item["Has Payment"] = "Yes";
item.Update();
}
else
{
item["Has Payment"] = "No";
item.Update();
}
}
}
}

No comments: