Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint...
-
Upload
juniper-mason -
Category
Documents
-
view
247 -
download
6
Transcript of Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint...
Microsoft® Official Course
Working with SharePoint Objects
Microsoft SharePoint 2013
SharePoint Practice
Module Overview
Understanding the SharePoint Object Hierarchy
Working with Sites and Webs•Working with Execution Contexts
Lesson 1: Understanding the SharePoint Object Hierarchy
The SharePoint Object Hierarchy
The SPFarm Class
Working with Services
Working with Service Applications
Discussion: Understanding the Service Application Architecture
Working with Web Applications
Site Collections and the SPSite Class• Individual Sites and the SPWeb Class
The SharePoint Object Hierarchy
SPFarm
SPService
SPWebApplication
SPSite
SPWeb
SPList
SPServiceApplication
All arrows represents one-
to-many relationships
The SPFarm Class
•Highest-level object in the hierarchy
•Represents farm-wide configuration
• Instantiate through the static Local property
•Use properties and methods to retrieve configuration settings
SPFarm farm = SPFarm.Local;
Guid farmID = SPFarm.Local.Id;Bool isAdmin = SPFarm.Local.CurrentUserIsAdministrator();
Working with Services
•Service classes• SPService represents a farm-scoped service• SPServiceInstance represents an instance of an SPService on a specific server
•The SPWebService class• Container for web applications• ContentService• AdministrationService
•Scope• Not available in SharePoint Online• Not available in sandboxed solutions or client-side code
Working with Service Applications
Service Application
SPServiceApplication
Service Application Proxy
SPServiceApplicationProxy
Web Application
SPWebApplication
Service Application Proxy Group
SPServiceApplicationProxyGroup
Discussion: Understanding the Service Application Architecture
•Why have service application proxies?
•Why have service application proxy groups?
Working with Web Applications
•Containers for site collections
•Map SharePoint content to IIS websites
•Represented by the SPWebApplication classvar contentService = SPWebService.ContentService;SPWebApplication webApp = contentService .WebApplications["Display Name"];webApp.MaximumFileSize = 75;webApp.Update();
Site Collections and the SPSite Class
•Container for individual sites
•Security boundary
•Deployment scope for many artifacts
•Various ways to instantiate:// Supply a URL.var site1 = new SPSite("http://team.contoso.com");
// From the parent SPWebApplication instance.var site2 = webApp.Sites["team.contoso.com"];
// From the execution context.var site3 = SPContext.Current.Site;
Individual Sites and the SPWeb Class
•Container for lists and libraries
•Container for child SPWeb objects
•Every site collection contains one root web
•Various ways to instantiate:
// From the parent SPSite instance.var web1 = site.RootWeb;var web2 = site.AllWebs["finance"];var web3 = site.OpenWeb("finance");
// From the execution context.var web4 = SPContext.Current.Web;
Lesson 2: Working with Sites and Webs
Managing Object Life Cycles
Retrieving and Updating Properties
Demonstration: Updating Properties•Creating and Deleting Sites and Webs
Managing Object Life Cycles
• SPSite and SPWeb objects are memory-intensive
• Developers must manage the object life cycle
•Disposal guidelines:• If you instantiated the object, dispose of it• If you referenced an existing object, do not dispose of it
•Disposal patterns:• try-catch-finally blocks• using blocks
Retrieving and Updating Properties
•Retrieving properties
•Updating properties
SPWeb web = SPContext.Current.Web;
// Retrieve a simple property.string title = web.Title;
// Retrieve a collection property.SPListCollection lists = web.Lists;foreach (SPList list in lists) { ... }
// Update various properties.web.Title = "New Title";web.Description = "A brand new description.";
// Write the changes to the content database.web.Update();
Demonstration: Updating Properties
In this demonstration, you will see an example of how to update the properties of an SPWeb object.
Creating and Deleting Sites and Webs
•Creating sites and webs• Call the Add method on a collection object
•Deleting sites and webs• Call the Delete or Recycle method on the object• You must still dispose of the object properly
SPSite site = webApp.Sites.Add("/sites/finance", @"CONTOSO\Administrator", "[email protected]");SPWeb web = site.AllWebs.Add("project1");
SPWeb web = site.OpenWeb("project1");web.Delete();web.Dispose();
Lab A: Working with Sites and Webs
Exercise 1: Working with Sites and Webs in Managed Code•Exercise 2: Working with Sites and Webs in Windows PowerShell.
Lab Scenario
The management team at Contoso has complained that project sites across the organization use a variety of different naming conventions. They have asked you to find an efficient way to update several site titles. In this lab, you will prototype two different approaches. First, you will use a visual Web Part to enumerate sites and enable users to update site properties. You will then experiment with performing the same procedure in Windows PowerShell.
Lab Review
Which approach would you recommend to the management team: the visual Web Part or the Windows PowerShell script? Why?
What happens if a user with insufficient permissions loads the Web Part? How would you work around any issues caused by insufficient permissions?•Enumerating sites and webs is computationally expensive. Why is this particularly problematic in a Web Part?
Lesson 3: Working with Execution Contexts
Understanding the SharePoint Context
Working with Users and Permissions
Discussion: Adapting Content for Different User Permissions•Manipulating the Execution Context
Understanding the SharePoint Context
•SPContext object
•Represents context of current HTTP request
•Provides a range of information:
•Only available when your code is invoked synchronously by an HTTP request
SPSite currentSite = SPContext.Current.Site;
SPWeb currentWeb = SPContext.Current.Web;
SPUser currentUser = SPContext.Current.Web.CurrentUser;
Working with Users and Permissions
•Verifying permissions programmatically
•Using security trimming
var web = SPContext.Current.Web;if(web.DoesUserHavePermissions( SPBasePermissions.ManageWeb)){ // Perform the update operation.}
<SharePoint:SPSecurityTrimmedControl runat="server" PermissionsString="ManageWeb"> <!-- Add child controls here --></SharePoint:SPSecurityTrimmedControl>
Discussion: Adapting Content for Different User Permissions
•When should you use code to adapt content to the permissions of the current user?
•When should you use the SPSecurityTrimmedControl to adapt content to the permissions of the current user?
Manipulating the Execution Context
•Use SPSecurity.RunWithElevatedPrivileges to run code using the system accountvar delegateDPO = new SPSecurity.CodeToRunElevated(DoPrivilegedOperation);SPSecurity.RunWithElevatedPrivileges(delegateDPO);
private void DoPrivilegedOperation() { // This method will run with elevated privileges.}
SPSecurity.RunWithElevatedPrivileges(delegate(){ // This code will run with elevated privileges.});
Lab B: Working with Execution Contexts
Exercise 1: Running Code with Elevated Privileges•Exercise 2: Adapting Content for Different User Permissions
Lab Scenario
In this lab, you will modify the visual Web Part you created in the previous exercise to make sure it renders correctly for all users. You want to display the list of webs to all users, so you will adapt the code that populates the list box to run with elevated privileges. However, you only want the update controls to be visible to users who have the permissions required to update web titles, so you will wrap the update controls in an SPSecurityTrimmedControl element.
Module Review and Takeaways
•Review Question(s)