Before you start developing you need a reference to "using Microsoft.SharePoint;" namespace.
Working with a list
Short description of some classes:
Site Organization Classes
SPContext -
SPWebCollection -
SPListCollection -
SPWeb - represents site;
SPSite - represents site collection;
SPList -
List Classes
SPField -
SPView -
SPListItem -
Getting the Current Website
SPWeb web = SPContext.Current.Web;
Site Collection Navigation
SPSite siteCollection = SPContext.Current.Site;
SPWeb rootWeb = siteCollection.RootWeb;
TreeNode rootNode = new TreeNode(rootWeb.Title);
treeSiteCollection.Nodes.Add(rootNode);
addSubWebsToTree(rootWeb, rootNode);
Create a list
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
SPList list;
try
{
list = web.Lists["Student Assignments"];
}
catch(Exception ex)
{
System.Guid listID
= web.Lists.Add("Student Assignments", "", SPListTemplateType.Tasks);
list = web.Lists[listID];
}
Creating a column in a list
SPFieldCollection fields = list.Fields;
SPField gradeField;
if (fields.ContainsField("Grade"))
{
gradeField = fields["Grade"];
}
else
{
fields.Add("Grade", SPFieldType.Number, false);
}
Adding Field to default view of the list
SPView defaultView = list.DefaultView;
if (!defaultView.ViewFields.Exists("Grade"))
{
defaultView.ViewFields.Add("Grade");
defaultView.Update();
}
Creating a new list item
SPListItemCollection items = list.Items;
SPListItem newItem = items.Add();
newItem["Title"] = "Homework 1";
newItem["DueDate"] = System.DateTime.Now;
newItem["Grade"] = 5; newItem.Update();
Saturday, January 10, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment