Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Development Platform Stack

Friday, May 11, 2012 5:07 PM

Session 2 Page 1

Data Access Technologies


Thursday, April 26, 2012 2:57 PM

Session 2 Page 2

Object Model
Saturday, May 12, 2012 4:05 PM

The Content Hierarchy


SPWebApplication has many members that can be used for developing administration functionality. Some of the more important members can help in the administration of the following: * Security policy * Alerts * Document conversions * Backups and restorations of the Web application * Logging of changes to the Web application * Outbound e-mail * External workflow participation * Use of the Windows Live MetaWeblog API * Use of the Recycle Bin * Unused site collections * Web.config file modifications

SPContentDatabase has a Sites property that holds all its child SPSite objects. SPContentDatabase has many members that can be used for developing administration functionality. Some of the more important members can help in the administration of the following: * Backup and restoration of the database * Upgrades of the database * Migration of (moving) the database * Repairs of the database * The database connection string and logon username and password * Limiting the number of site collections Most of the properties of SPContentDatabase are read-only. SPSite many members that you can use to develop administration functionality. Some of the more important members can help in the administration of the following: * The maximum length of Web site URLs * RSS feeds * Auditing * Dead Web sites * Features * Ownership of the site collection * Size quotas for the site collection * Site usage data * Workflows in the site collection * Templates and Web part galleries * Self-service site creation

The Physical Objects hierarchy

Session 2 Page 3

The Services Hierarchy

Session 2 Page 4

Site Architecture and OM


Saturday, May 12, 2012 7:04 PM

Session 2 Page 5

Perform operations using Browser


Create Web application Create Site Collection "/" Create site collection "/sites/" Create list, libraries, views Create templates Content types Associate template Add site columns Add content type to library and set default

Session 2 Page 6

Project Template Type


Thursday, May 17, 2012 11:22 PM

Session 2 Page 7

Session 2 Page 8

Server Side Object Model

Session 2 Page 9

Customization Vs. Development

Customization
Changes that are made to a SharePoint environment using the browser interface or a tool like SharePoint Designer. Customizations are changes that get stored in a SharePoint content database and have no relationship to a file on the filesystem.

Development
Usually refers to the creation of solution packages and features that deploy assemblies and/ or other files to the SharePoint server(s) in your farm. These files might add content to a content database, but they originate as fi les on the file system. Development is often done using a tool like Visual Studio.

Session 2 Page 10

Commonly Used Classes

Class Name SPFarm SPWebApplication SPSite SPWeb SPList SPListItem SPFolder SPFile SPGroup SPUser SPContentType SPField SPView

Description The very top - level object, providing access to configuration information for the entire SharePoint farm A load - balanced IIS web application that is part of the SharePoint farm A site collection inside a web application An individual site within a site collection A list within a particular site An item in a SharePoint list A folder within a SharePoint site A document stored in a SharePoint list A SharePoint group A SharePoint user A content type A column (either a site column or a list column, depending on its location) The view on a particular list

Note SPSite object represents a site collection, not a site.

Session 2 Page 11

Context and Constructor


Friday, May 18, 2012 9:41 PM

using (SPSite hrSite = new SPSite("http://intranet/sites/hr")) { SPWeb healthWeb = hrSite.OpenWeb("health"); string title = healthWeb.Title; }

Session 2 Page 12

Console Application
Friday, May 18, 2012 10:03 PM

Session 2 Page 13

Object Disposal
Friday, May 18, 2012 9:42 PM

Its often necessary to explicitly dispose of SharePoint objects like SPSite and SPWeb to prevent them from remaining active and taking up an increasing amount of server memory. Using Block Explicitly in Try/catch and Finally
using (SPSite intranetSiteCollection = new SPSite("http://intranet/")) { SPWebCollection websCollection = siteCollection.AllWebs; foreach (SPWeb web in websCollection) { Console.WriteLine(web.Title); web.Dispose(); } Console.Read(); }

When retrieving SPSite or SPWeb objects directly from the SPContext.Current object, you should not explicitly dispose of the objects SPSite siteCollection = SPContext.Current.Site; SPWeb topLevelSite = siteCollection.RootWeb; SPWebCollection webs = topLevelSite.Webs; SPWeb subsiteA = webs["Subsite A"];

Session 2 Page 14

Examples
Friday, May 18, 2012 9:55 PM

Open Grand child web using (SPSite sc = new SPSite("http://intranet/"); { SPWeb rootWeb = sc.OpenWeb(); rootWeb.Dispose(); SPWeb web = sc.OpenWeb("subsite1/subsite2"); web.Dispose(); } List

SPListCollection lists = subsiteA.Lists; SPList myList = lists["My List"];

ListItem SPListItem myListItem = myList["My List Item"]; Update MasterPage SPWeb currentWeb = SPContext.Current.Web; currentWeb.MasterUrl = "/_catalogs/masterpage/Custom.master"; currentWeb.Update(); Create New Web, List , ListItem
SPWeb newWeb = SPContext.Current.Web.Webs.Add("MyNewSubsite"); SPList newList = newWeb.Lists.Add("New List", "Another list", SPListTemplateType.GenericList); SPListItem newListItem = newList.Items.Add(); newListItem["Title"] = "New List Item"; newListItem.Update();

Deletes a site collection used for testing

using (SPSite siteCollection = new Site("http://intranet/sites/test")) { siteCollection.Delete(); }

Deletes a site in the current site collection

using (SPWeb testSite = SPContext.Current.Site.OpenWeb("test")) { testSite.Delete(); }

Deletes a list called Invoices in the current site


Deletes a list item with a title of My List Item

SPContext.Current.Web.Lists["Invoices"].Delete();

SPList myList = SPContext.Current.Web.Lists["My List"]; myList["My List Item"].Delete();

Session 2 Page 15

File and Folders


Friday, May 18, 2012 10:03 PM

List Folders StringBuilder folderName = new StringBuilder(); using (SPSite site = new SPSite("http://intranet/")) { using (SPWeb web = site.RootWeb) { foreach (SPFolder folder in web.Folders) { folderName.Append(folder.Name); folderName.Append(" "); folderName.Append(folder.SubFolders.Count); folderName.Append("<br>"); } } } Add Folder SPContext.Current.Web.Folders.Add("NewFolder");

Upload File
//Instantiate a new site collection object using (SPSite site = new SPSite("http://intranet/")) { //Get the list object inside the subsite SPWeb web = site.OpenWeb("sites/HR"); SPFolder listFolder = web.Lists["Documents"].RootFolder; string folderUrl = web.ServerRelativeUrl + listFolder.Url; string fileUrl = folderUrl + "/Application.docx"; FileStream stream = System.IO.File.Open(@"C:\Application.docx", FileMode.Open); byte[] contents = new byte[stream.Length]; stream.Read(contents, 0, (int)stream.Length); stream.Close(); SPFile file = web.Files.Add(fileUrl, contents); SPListItem listItem = file.Item; listItem["Title"] = "Job Application"; listItem.Update(); web.Dispose(); }

Session 2 Page 16

You might also like