Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. SharePoint API and Development in ASP.NET Creating “Hello World” application Working with.

Similar presentations


Presentation on theme: "Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. SharePoint API and Development in ASP.NET Creating “Hello World” application Working with."— Presentation transcript:

1 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. SharePoint API and Development in ASP.NET Creating “Hello World” application Working with WSS object model Site collection organization List manipulation Security Administration Working with Office object model User profiles Code performance and security Object model overview

2 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. ASP.Net Website with SharePoint Object Model Create ASP.NET website in the following url: http:// /_layouts/

3 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. References to SharePoint DLLs Add a reference to Microsoft.SharePoint.dll and Microsoft.Office.Server.dll in Local_Drive:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI

4 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Demo: “Hello World” ASP.NET Application public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Put user code to initialize the page here Response.Write("Hello there!"); } Visual Studio short cuts: F7 - Switch between Code (Default.aspx.cs) and Designer (Default.aspx in Design view) Ctrl+ PgDw - View HTML Source (Default.aspx in HTML view) Visual Studio short cuts: F7 - Switch between Code (Default.aspx.cs) and Designer (Default.aspx in Design view) Ctrl+ PgDw - View HTML Source (Default.aspx in HTML view)

5 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Site Collection Organization In this section we will cover the basics of working with site collections: Getting current website Getting lists for the website Getting all websites in a site collection

6 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Getting the Current Website using Microsoft.SharePoint; SPWeb web = SPContext.Current.Web; labelMessage.Text = web.Title;

7 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Lists and List Items for the Current Website TreeNode webNode = new TreeNode(web.Title); treeWeb.Nodes.Add(webNode); SPListCollection lists = web.Lists; foreach (SPList list in lists) { if (list.Fields.ContainsField(“Title")) { TreeNode listNode = new TreeNode(list.Title); webNode.ChildNodes.Add(listNode); SPListItemCollection items = list.Items; foreach (SPListItem item in items) { TreeNode itemNode = new TreeNode(item.Title); listNode.ChildNodes.Add(itemNode); }

8 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Site Collection Navigation SPSite siteCollection = SPContext.Current.Site; SPWeb rootWeb = siteCollection.RootWeb; TreeNode rootNode = new TreeNode(rootWeb.Title); treeSiteCollection.Nodes.Add(rootNode); addSubWebsToTree(rootWeb, rootNode);

9 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Site Collection Navigation Recursion private void addSubWebsToTree(SPWeb currentWeb, TreeNode currentNode) { foreach (SPWeb subWeb in currentWeb.Webs) { TreeNode subWebNode = new TreeNode(subWeb.Title); currentNode.ChildNodes.Add(subWebNode); addSubWebsToTree(subWeb, subWebNode); subWeb.Dispose(); }

10 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Demo: Site Organization Classes 1.Use SPContext, SPSite, SPWeb, SPWebCollection, SPListCollection, and SPList classes

11 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Lab: Exercises 1-2 Perform exercises 1 - 2 from Basic SharePoint API Development Lab

12 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. List Manipulation In this section we cover how to create new data Create a list Create columns in the list Modify a view for the list Add new list item for the list

13 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Creating a list SPWeb web = SPContext.Current.Web; web.AllowUnsafeUpdates = true; SPList list; try { list = web.Lists["Student Assignments"]; } catch(Exception ex) { System.Guid listID = web.Lists.Add("Student Assignments", "", SPListTemplateType.Tasks); list = web.Lists[listID]; }

14 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Creating a column in a list SPFieldCollection fields = list.Fields; SPField gradeField; if (fields.ContainsField("Grade")) { gradeField = fields["Grade"]; } else { fields.Add("Grade", SPFieldType.Number, false); }

15 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Adding Grade field to default view of the list SPView defaultView = list.DefaultView; if (!defaultView.ViewFields.Exists("Grade")) { defaultView.ViewFields.Add("Grade"); defaultView.Update(); }

16 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Creating a new list item SPListItemCollection items = list.Items; SPListItem newItem = items.Add(); newItem["Title"] = "Homework 1"; newItem["DueDate"] = System.DateTime.Now; newItem["Grade"] = 5; newItem.Update();

17 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Demo: List Classes 1.Use SPList, SPField, SPView, and SPListItem classes

18 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Lab: Exercises 3 - 5 Perform exercises 3 - 5 from Basic SharePoint API Development Lab

19 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Free Style Exercise: List Manipulation Free Style Exercise 1.Go through all sub sites below the current one 2.Create a list called “Inventory” with “generic list” template (if the list does not exist) 3.Add a column “Quantity” to the list (if the column does not exist) 4.Add “Quantity” column to the default view( if the column is already not included in view)

20 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Security In this section we cover how to work with security features: Checking if the website has unique permissions Listing all domain users and groups Listing all SharePoint groups Listing all Permissions levels Adding user to a SharePoint group Setting unique permissions for a folder

21 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Security Permission Inheritance SPSite site = SPContext.Current.Site; SPWeb web = SPContext.Current.Web; // Test to see if the site has unique permissions if (web.HasUniqueRoleAssignments) { Response.Write(web.Title + " does not inherit permissions "); } else { Response.Write(web.Title + " inherits permissions "); }

22 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Domain User and Group Security Principals // All domain users and groups // that are Security Principals for the website // This is the list you see in /_layouts/user.aspx SPUserCollection users = web.Users; Response.Write(" All domain user principals in this site: " + " "); foreach (SPUser user in users) { Response.Write(user.Name + " "); } // All domain users and groups within the site collection users = web.SiteUsers; Response.Write(" All users in this site collection: " + " "); foreach (SPUser user in users) { Response.Write(user.Name + " "); }

23 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. SharePoint Group Security Principals // SharePoint Groups that are Security Principals on this website SPGroupCollection groups = web.Groups; Response.Write(" All SharePoint groups in this site: " + " "); foreach (SPGroup group in groups) { Response.Write(group.Name + " "); } // All SharePoint Groups in this site collection groups = web.SiteGroups; Response.Write(" All SharePoint groups in this site collection: " + " "); foreach (SPGroup group in groups) { Response.Write(group.Name + " "); }

24 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. SharePoint Group Users // All users in "Students" SharePoint group users = web.Groups["Students"].Users; Response.Write(" All users in \"Students\" SharePoint group: " + " "); foreach (SPUser user in users) { Response.Write(user.Name + " "); }

25 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Role Definitions // Role definitions are represented as "Permissions Levels" // in user interface SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions; Response.Write(" All role definitions in this site: " + " "); foreach (SPRoleDefinition roleDefinition in roleDefinitions) { Response.Write(roleDefinition.Name + " "); }

26 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Role Assignments For the Site SPRoleAssignmentCollection webRoleAssignments = web.RoleAssignments; Response.Write(" All role assignments in this site: " + " "); foreach (SPRoleAssignment webRoleAssignment in webRoleAssignments) { Response.Write(webRoleAssignment.Member.Name + " " + webRoleAssignment.RoleDefinitionBindings[0].Name + " " + webRoleAssignment.Parent.ToString() + " "); }

27 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Adding User to a SharePoint Group web.AllowUnsafeUpdates = true; SPGroup studentsGroup = web.Groups["Students"]; SPUser userToAdd = web.SiteUsers["pilothouse\\testgroup"]; studentsGroup.AddUser(userToAdd); Response.Write("Added user successfully");

28 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Setting Unique Folder Permissions SPPrincipal principal = userToAdd; SPFolderCollection submissions = web.GetFolder(“Submissions").SubFolders; SPFolder newFolder = submissions.Add("testfolder"); SPListItem folderItem = newFolder.Item; folderItem.BreakRoleInheritance(true); folderItem.Update(); SPRoleAssignment folderRoleAssignment = new SPRoleAssignment(principal); SPRoleAssignmentCollection folderRoleAssignments = folderItem.RoleAssignments; SPRoleDefinitionBindingCollection folderRoleDefBindings = folderRoleAssignment.RoleDefinitionBindings; folderRoleDefBindings.Add(roleDefinitions["Contribute"]); folderRoleAssignments.Add(folderRoleAssignment);

29 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Demo: Security Classes 1.Use SPGroup, SPUser, SPRoleAssignment, and SPRoleDefinition classes

30 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Free Style Exercise: Security Free Style Exercise 1.Create a new list and give "Students" SharePoint group "Contributor" permissions in that list 2.Remove all other role assignments from that list

31 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Administration In this section we cover how to work with Administration objects: Listing all SharePoint web applications Listing all content databases for a web application Listing all site collections for a content database

32 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Iterating Through Web Applications SPWebApplicationCollection webApplications = SPWebService.ContentService.WebApplications; foreach (SPWebApplication webApplication in webApplications) { TreeNode webAppNode = new TreeNode(webApplication.Name); treeGlobal.Nodes.Add(webAppNode); }

33 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Iterating Through Content Databases and Sites SPContentDatabaseCollection contentDatabases = webApplication.Content Databases; foreach (SPContentDatabase contentDatabase in contentDatabases) { TreeNode contentDBNode = new TreeNode(contentDatabase.Name); webApplNode.ChildNodes.Add(contentDBNode); SPSiteCollection sites = contentDatabase.Sites; foreach (SPSite site in sites) { TreeNode siteNode = new TreeNode(site.Url); contentDBNode.ChildNodes.Add(siteNode); site.Dispose(); }

34 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Demo: Administration Classes 1.Use SPWebService, SPFarm, SPWebApplication, and SPContentDatabase classes

35 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Microsoft.Office.Server.UserProfiles Example using Microsoft.Office.Server.UserProfiles; UserProfileManager upm = new UserProfileManager(ServerContext.Current); UserProfile up = upm.GetUserProfile("spstraining\\administrator"); Response.Write("work email is " + up[PropertyConstants.WorkEmail].Value + " "); Response.Write("state is " + up["State"].Value);

36 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Demo: User Profile Classes 1.Use UserProfileManager and UserProfile classes from Microsoft.Office.Server.UserProfiles namespaces

37 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Code Performance Use SPWeb web = SPContext.Current.Web; SPList list = web.Lists["Tasks"]; list.Title=“deployment"; list.Description=“documenting our deployment"; list.Update(); Instead of: SPWeb web = SPContext.Current.Web; web.Lists[“Tasks"].Title = “deployment task"; web.Lists["Tasks"].Description = “Documenting our deployment"; web.Lists["Tasks"].Update(); Because it minimizes the number of trips to the database.

38 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Use Dispose To Free Up Memory Use foreach(SPSite site in sites) { //code site.Dispose(); } Or for(i = 0; i < sites.Count; i ++) { using(SPSite site = sites[i]) { //code } In order to avoid retaining the objects in memory.

39 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Code Security Use HtmlEncode method in Microsoft.SharePoint.Utilities.SPEncode to prevent execution of malicious script For example when someone enters the following title for the list “ alert(); ” Response.Write(list.Title) would cause a message box to pop up.

40 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Application infrastructure Web.config for storing properties Localizations using.NET standards Windows installer or xcopy deployment

41 Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. Demo: Examining Object Model Using the SDK 1.WSS object model reference 2.Office object model reference


Download ppt "Copyright © 2006 Pilothouse Consulting Inc. All rights reserved. SharePoint API and Development in ASP.NET Creating “Hello World” application Working with."

Similar presentations


Ads by Google