Wednesday, May 12, 2010

Introducing the SharePoint Object Model

As an alternative to programming against the SharePoint web services you can use the SharePoint object model. The object model can be used when the application will run on the server where SharePoint is installed (such as a console or WinForm application) or in assemblies that are run within a site (such as a Web Part or Web User Controls).

How to the SharePoint Object Model ?
First, you will need to create a reference to “SharePoint.dll” assembly in your Visual Studio 2005/2008 project. This is located at: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll

1. Get Attachment Name                                                                                                                      
public string getAttachmentName(string ItemID)
{
string strDocName = "";
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList List = myWeb.Lists["MyList"];
SPListItem splstItem = List.Items.GetItemById(Convert.ToInt32(ItemID));
if (splstItem.Attachments.Count > 0)
{
SPAttachmentCollection spColl = splstItem.Attachments;
strDocName = spColl[0];
}
return strDocName;
}
2. Get Items count from the list using SPQuery
public int GetItemsCount(string strStatus)
{
StringBuilder strQuery = new StringBuilder();
strQuery.Append("<Where>");
strQuery.Append("<Eq>");
strQuery.Append("<FieldRef Name='Status' />");
strQuery.Append("<Value Type='Choice'>" + strStatus+ "</Value>");
strQuery.Append("</Eq>");
strQuery.Append("</Where>");

SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList List = myWeb.Lists["MyList"];
SPQuery oSPQuery = new SPQuery();
oSPQuery.Query = strQuery.ToString();
SPListItemCollection items = List.GetItems(oSPQuery);
return items.Count;
}
3. Insert New Item in the list
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList myList = myWeb.Lists["MyList"];
SPListItem item = myList.Items.Add();
item["Title"] = "Amit Phule";
item.Update();
4. Get List Items count
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList List = myWeb.Lists["MyList"];
return List.Items.Count;
5. Update List Item from Item ID
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList List = myWeb.Lists["DCCDocs"];
SPListItem oListItem = List.Items.GetItemById(Convert.ToInt32(ItemID));
oListItem["Title"] = "New Title";
oListItem.Update();
6. Delete List Item from Item ID
Code Here
7. Send Email from SharePoint
private void SendMail(string To, string Subject, string Body)
{
SPSite mySite = SPContext.Current.Site;
SPWeb oWeb = mySite.OpenWeb();
SPUtility.SendEmail(oWeb, true, true, To, Subject, Body);
}
8. Get List Item from CAML Query
StringBuilder strQuery = new StringBuilder();
strQuery.Append("<Where>");
strQuery.Append("<Eq>");
strQuery.Append("<FieldRef Name='Status' />");
strQuery.Append("<Value Type='Choice'>" + strStatus+ "</Value>");
strQuery.Append("</Eq>");
strQuery.Append("</Where>");
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList List = myWeb.Lists["MyList"];
SPQuery oSPQuery = new SPQuery();
oSPQuery.Query = strQuery.ToString();
SPListItemCollection items = List.GetItems(oSPQuery);
foreach (SPItem item in items)
{
  TxtTitle.text=item["Title"].ToString();
}
9. Get User Groups
private Boolean IsCurrentUserGroup(string strGroupName)
{
Boolean isGroupPresent=false;
SPUser oCurrentUser = SPContext.Current.Web.CurrentUser;
foreach (SPGroup oSPGroup in oCurrentUser.Groups)
{
if (oSPGroup.Name.Equals(strGroupName))
isGroupPresent= true;
}
return isGroupPresent;
}
10. Title
Code Here


No comments:

Post a Comment