Create a List Item (70-541)
Posted: 18/12/2009 Filed under: Certification, Development | Tags: 70-541, Certification Leave a comment »SPSite oSite = new SPSite("http://rootsite");
SPWeb oWeb = oSite.AllWebs["subsite"];
SPListItemCollection oItems = oWeb.Lists["list title"].Items;
// Create a New Item
SPListItem oItem = oItems.Add();
oItem["field"] = "value";
// Code that populates other fields
oItem.Update();
// Clean up
oWeb.Dispose();
oSite.Dispose();
Delete a List Item (70-541)
Posted: 18/12/2009 Filed under: Certification, Development | Tags: 70-541, Certification Leave a comment »SPSite oSite = new SPSite("http://rootsite");
SPWeb oWeb = oSite.AllWebs["subsite"];
SPListItemCollection oItems = oWeb.Lists["list title"].Items;
// Enumerate through all list items
foreach (SPListItem oItem in oItems)
{
// Delete a List Item
if(oItem["field"].ToString() == "value")
{
oItem.Delete()
return;
}
}
// Clean up
oWeb.Dispose();
oSite.Dispose();
Note, the return statement right after the item has been deleted. You do not want to keep looping through all list items once we’ve deleted the one required, as you will be thrown an exception.
Update a List Item (70-541)
Posted: 18/12/2009 Filed under: Certification, Development | Tags: 70-541, Certification Leave a comment »SPSite oSite = new SPSite("http://rootsite");
SPWeb oWeb = oSite.AllWebs["subsite"];
SPListItemCollection oItems = oWeb.Lists["list title"].Items;
// Enumerate through all list items
foreach (SPListItem oItem in oItems)
{
// Update a List Item
if(oItem["field"].ToString() == "value")
{
oItem["field"] = "new value";
oItem.Update();
return;
}
}
// Clean up
oWeb.Dispose();
oSite.Dispose();
Enumerate List Items (70-541)
Posted: 18/12/2009 Filed under: Certification, Development | Tags: 70-541, Certification Leave a comment »SPSite oSite = new SPSite("http://rootsite");
SPWeb oWeb = oSite.AllWebs["subsite"];
SPListItemCollection oItems = oWeb.Lists["list title"].Items;
// Enumerate Through All List Items
foreach (SPListItem oItem in oItems)
{
// Additional Code
}
// Clean Up
oWeb.Dispose();
oSite.Dispose();
Remove a User from a Site Group (70-541)
Posted: 17/12/2009 Filed under: Certification, Development | Tags: 70-541, Certification Leave a comment »SPSite oSite = new SPSite("http://rootsite");
SPWeb oWeb = oSite.AllWebs["subsite"];
SPGroup oGroup = oWeb.Groups["securitygroup"];
SPUser oUser = oGroup.Users["domain\\loginname"];
oGroup.RemoveUser(oUser);
oGroup.Update();
// Clean Up
oWeb.Dispose();
oSite.Dispose();
Add a User to a Site Group (70-541)
Posted: 17/12/2009 Filed under: Certification, Development | Tags: 70-541, Certification 2 Comments »SPSite oSite = new SPSite("http://rootsite");
SPWeb oWeb = oSite.AllWebs["Subsite"];
SPGroup oGroup = oWeb.Groups["SecurityGroup"];
oGroup.AddUser("domain\\loginname", "emailaddress", "username", "notes on user");
// Alternatively use oGroup.AddUser(SPUser object);
oGroup.Update();
// Clean Up
oWeb.Dispose();
oSite.Dispose();
Note: When specifying a user login name, don’t forget the domain name before (e.g: domain\\loginname). It is also not necessary to specify a user’s email address or notes on the user.