Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

Similar presentations


Presentation on theme: "Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice."— Presentation transcript:

1 Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice

2 Module Overview Understanding the SharePoint Object Hierarchy Working with Sites and Webs Working with Execution Contexts

3 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

4 The SharePoint Object Hierarchy SPFarm SPService SPWebApplication SPSite SPWeb SPList SPServiceApplication All arrows represents one- to-many relationships

5 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();

6 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

7 Working with Service Applications Service Application SPServiceApplication Service Application SPServiceApplication Service Application Proxy SPServiceApplicationProxy Service Application Proxy SPServiceApplicationProxy Web Application SPWebApplication Web Application SPWebApplication Service Application Proxy Group SPServiceApplicationProxyGroup Service Application Proxy Group SPServiceApplicationProxyGroup

8 Discussion: Understanding the Service Application Architecture Why have service application proxies? Why have service application proxy groups?

9 Working with Web Applications Containers for site collections Map SharePoint content to IIS websites Represented by the SPWebApplication class var contentService = SPWebService.ContentService; SPWebApplication webApp = contentService.WebApplications["Display Name"]; webApp.MaximumFileSize = 75; webApp.Update();

10 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;

11 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;

12 Lesson 2: Working with Sites and Webs Managing Object Life Cycles Retrieving and Updating Properties Demonstration: Updating Properties Creating and Deleting Sites and Webs

13 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

14 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();

15 Demonstration: Updating Properties In this demonstration, you will see an example of how to update the properties of an SPWeb object.

16 Demonstration: Updating Properties

17 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", "administrator@contoso.com"); SPWeb web = site.AllWebs.Add("project1"); SPWeb web = site.OpenWeb("project1"); web.Delete(); web.Dispose();

18 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.

19 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.

20 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?

21 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

22 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;

23 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">

24 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?

25 Manipulating the Execution Context Use SPSecurity.RunWithElevatedPrivileges to run code using the system account var 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. });

26 Lab B: Working with Execution Contexts Exercise 1: Running Code with Elevated Privileges Exercise 2: Adapting Content for Different User Permissions

27 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.

28 Module Review and Takeaways Review Question(s)

29 Module Review and Takeaways Review Question(s)

30 Module Review and Takeaways Review Question(s)


Download ppt "Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice."

Similar presentations


Ads by Google