Wednesday 14 May 2014

WSS object model

In this post I would like to explain the basic objects of SharePoint and how to create and retrieve the SharePoint content programmatically using object model.

Create a SharePoint WebApplciation
Namespace: Microsoft.SharePoint.Administration
The following example uses the SPWebApplicationBuilder to create a new SharePoint Web application on port 999.
SPWebApplicationBuilder webAppBuilder = new SPWebApplicationBuilder(SPFarm.Local);
int myPort = 999;
webAppBuilder.Port = myPort;
SPWebApplication newApplication = webAppBuilder.Create();

newApplication.Provision();

Retrieve WebApplications

SPWebApplication webapp = SPWebService.ContentService.WebApplications[WebAppId];
WebAppId is WebApplication id, we can pass web application name also.

SPSite mySiteCollection = newApplication.Sites.Add("/", "DOMAIN\\UserAlias", "user@microsoft.com");

mySiteCollection.Close();

Provision which place the web application with content database means which gives the necessary things.

Creating a Site using object model:
Namespace: Microsoft.SharePoint
SPSite objSPSite = new SPSite("");
SPWeb mySite = objSPSite.OpenWeb();
mySite.AllowUnsafeUpdates = true;

SPWebCollection subSites = mySite.Webs;
string currentTemplate = mySite.WebTemplate;

string siteUrl = "Test";
string siteTitle = "Test";
string siteDescription = "Test Site";

subSites.Add(siteUrl, siteTitle, siteDescription, 1033,
currentTemplate, true, false);
Delete a Site:
SPSite objSPSite = new SPSite("");
SPWeb mySite = objSPSite.OpenWeb("Test");
mySite.AllowUnsafeUpdates = true;

mySite.Delete();
Craeting a SiteCollection:
Namespace: Microsoft.SharePoint.Administration
SPWebApplication objSPWebApplication = SPContext.Current.Site.WebApplication;
SPSiteCollection objSPSiteCollection = objSPWebApplication.Sites;

objSPSiteCollection.Add("http://Site/sites/Test", "Suman Hasnabad", "sumanh@ymail.com");

Create a List
SPSite objSPSite = new SPSite("");
SPWeb objSPWeb = objSPSite.OpenWeb("Project");
objSPWeb.AllowUnsafeUpdates = true;

SPListTemplate objSPListTemplate = objSPWeb.ListTemplates["Custom List"];
objSPWeb.Lists.Add("My List", "This is a Custom list",
objSPListTemplate);

Delete a List
SPSite objSPSite = new SPSite("");
SPWeb objSPWeb = objSPSite.OpenWeb("Project");
objSPWeb.AllowUnsafeUpdates = true;

SPList objSPList=objSPWeb.Lists["My List"];
objSPList.Delete();

Adding a List Items
objSPWeb.AllowUnsafeUpdates = true;
SPList objSPList = objSPWeb.Lists["Sample"];
SPListItem objSPListItem = objSPList.Items.Add();

objSPListItem["Test1"] = ddlTest1.SelectedValue;
objSPListItem["Test2"] = txtTest2.Value;
objSPListItem["Test3"] = txtTest3.Text.Trim();
objSPListItem["Test4"] = ddlTest4.SelectedValue;
objSPListItem.Update();

Retrieving the files from folders and copy to the other folder
SPSite objSPSite = new SPSite("");
SPWeb objSPWeb = objSPSite.OpenWeb("Project");

SPFolder objSPFolder = objSPWeb.GetFolder("Test");
SPFileCollection objSPFileCollection = objSPFolder.Files;

foreach (SPFile objSPFile in objSPFileCollection)
{
Response.Write(objSPFile.Name.ToString());
objSPFile.CopyTo("Demo/" + objSPFile.Name);
}

Using Web Services How to retrieve the List and List Items
Step 1: Add Web Reference to the project in the URL location as bellow.
http://SiteURL/_vti_bin/lists.asmx?wsdl
Step 2:
WebReference.Lists objLists = new ListWebservices.WebReference.Lists();
objLists.Credentials = System.Net.CredentialCache.DefaultCredentials;

System.Xml.XmlNode objXmlNode = objLists.GetListCollection();
foreach (System.Xml.XmlNode node in objXmlNode)
{
lbllists.Text = node.Attributes["Title"].Value;
}
By using GetListItems(listName, viewName,query,viewFields,rowLimit,queryOptions,webid)we can retrieve the List Items.
Using UpdateListItems (listname, XMl update)

Create List Items With Attachments

private void CreateListItemsWithAttachments(SPListCollection listCollection)
{
for (int listIndex = 0; listIndex < listCollection.Count; listIndex++)
{
if (listCollection[listIndex].Title.Contains("List with attachments"))
{
for (int itemCount = 1; itemCount <= Convert.ToInt32(txtNoOfItemsPerList.Text); itemCount++)
{
SPListItem listItem = listCollection[listIndex].Items.Add();
listItem["Title"] = "Item" + " " + itemCount;

byte[] byteArrayContents = ReadByteArrayFromFile(Constants.FilName);
SPAttachmentCollection attachments = listItem.Attachments;

// Add attchments to list items based on Number of attachments entered by user
for (int attachmentCount = 1; attachmentCount <= Convert.ToInt32(txtNoOfAttachments.Text); attachmentCount++)
{
attachments.Add("Test", byteArrayContents);
}

listItem.Update();
}
}
}
}


Upload Documents Into DocLibrary
private void UploadDocumentsIntoDocLibrary(SPWeb web, SPListCollection listCollection)
{
FileStream fStream = File.OpenRead(Constants.FilName);
byte[] content = new byte[fStream.Length];
fStream.Read(content, 0, (int)fStream.Length);
fStream.Close();

if (!string.IsNullOrEmpty(txtNoOfDocLib.Text))
{
for (int listIndex = 0; listIndex < listCollection.Count; listIndex++)
{
if (listCollection[listIndex].BaseTemplate == SPListTemplateType.DocumentLibrary)
{
for (int itemCount = 1; itemCount <= Convert.ToInt32(txtNoOfItemsPerList.Text); itemCount++)
{
try
{
web.Files.Add(web.Url + @"\" + listCollection[listIndex].Title + @"\" + "Test.Doc" + " " + itemCount, content);
}
catch (SPException e)
{
if (e.ErrorCode == -2130575257)
{
//MessageBox.Show("File already exists in Document Library:" + " " + listCollection[listIndex].Title + "" + "Please click ok to upload documents in other doc library");
}
}
}
}
}
}
}

///




/// This method is used to read data from file in byte format
///

/// File Name
/// Byte array
private byte[] ReadByteArrayFromFile(string fileName)
{
byte[] byteArrayContents = null;
FileStream fileStream = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
long numBytes = new FileInfo(fileName).Length;
byteArrayContents = binaryReader.ReadBytes((int)numBytes);
return byteArrayContents;
}

No comments:

Post a Comment