+ All Categories
Home > Documents > Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint...

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

Date post: 22-Dec-2015
Category:
Upload: juniper-mason
View: 237 times
Download: 6 times
Share this document with a friend
Popular Tags:
27
Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice
Transcript
Page 1: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

Microsoft® Official Course

Working with SharePoint Objects

Microsoft SharePoint 2013

SharePoint Practice

Page 2: 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

Page 3: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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

Page 4: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

The SharePoint Object Hierarchy

SPFarm

SPService

SPWebApplication

SPSite

SPWeb

SPList

SPServiceApplication

All arrows represents one-

to-many relationships

Page 5: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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

Page 6: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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

Page 7: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

Working with Service Applications

Service Application

SPServiceApplication

Service Application Proxy

SPServiceApplicationProxy

Web Application

SPWebApplication

Service Application Proxy Group

SPServiceApplicationProxyGroup

Page 8: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

Discussion: Understanding the Service Application Architecture

•Why have service application proxies?

•Why have service application proxy groups?

Page 9: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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

Page 10: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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;

Page 11: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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;

Page 12: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

Lesson 2: Working with Sites and Webs

Managing Object Life Cycles

Retrieving and Updating Properties

Demonstration: Updating Properties•Creating and Deleting Sites and Webs

Page 13: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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

Page 14: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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

Page 15: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

Demonstration: Updating Properties

In this demonstration, you will see an example of how to update the properties of an SPWeb object.

Page 16: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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

Page 17: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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.

Page 18: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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.

Page 19: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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?

Page 20: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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

Page 21: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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;

Page 22: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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>

Page 23: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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?

Page 24: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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.});

Page 25: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

Lab B: Working with Execution Contexts

Exercise 1: Running Code with Elevated Privileges•Exercise 2: Adapting Content for Different User Permissions

Page 26: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

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.

Page 27: Microsoft ® Official Course Working with SharePoint Objects Microsoft SharePoint 2013 SharePoint Practice.

Module Review and Takeaways

•Review Question(s)


Recommended