Posts

Showing posts with the label Sharepoint Coding

Creating Views in SharePoint Using the object model

Using the object model we can create a SharePoint views programmatically. The query for the view uses an SPQuery which uses CAML. //Define an array to hold the columns required in the view String[] colArray = new String[] { "FName", "LName", "Email” }; //Add the array to a string collection StringCollection colCollection = new StringCollection(); colCollection.AddRange(colArray); //Define the query for the view String viewQry = “Your CAML Query Here’; HTML clipboard //Define the new view & Add to the List (100 is the item limit, true is allow paging and false is to set as the default view) SPView newView = YourList.Views.Add(“View Title”, colCollection, viewQry, 100, true, false);

How to programmatically Create SPListItem in a SPFolder

Sample Code to Create SPListItem in a SPFolder SPSite mySite = new SPSite( "http://[ServerName/sitename]" ); SPWeb web = mySite.OpenWeb(); SPList list = web.Lists[ "ListName" ]; SPFolder f = web.GetFolder( "http://[ServerName/sitename]/Lists/ListName/FolderName" ); if (f.Exists) { SPListItemCollection itemColl = list.Items; SPListItem item = itemColl.Add(f.ServerRelativeUrl , SPFileSystemObjectType.File, null ); item[ "Title" ] = "Added from Code" ; item.Update(); }

Diff B/w SPSite & SPWeb

SPSite class : SPSite Represents a collection of sites on a virtual server, including a top-level site and all its subsites. Each SPSite object, or site collection, is represented within an SPSiteCollection object that consists of the collection of all site collections on the virtual server. SPSite.OpenWeb Method: Returns the site that is located at the specified server-relative or site-relative URL. SPWeb class : SPWeb Simply represents Windows SharePoint Services Web site. Code sample to create a site: SPSite mySite = new SPSite("http://servername/"); SPWeb myWeb = mySite.AllWebs["Site_Name"]; SPWeb myRootWeb = mySite.RootWeb; Note: SPSite and SPWeb inherit directly from System.Object Calling Dispose ensures all resources used by those objects are immediately released.