210 likes | 226 Views
Learn how to programmatically create, delete, and manage sites and lists in SharePoint, including securing sites and adding role definitions. Explore manipulating server-side objects.
E N D
Module 4 Working with SharePoint Objects on the Server
Module Overview • Working with SharePoint Sites Programmatically • Working with SharePoint Lists Programmatically • Working with List Data Programmatically • Lab: Creating and Manipulating Server-Side Objects
Lesson 1: Working with SharePoint Sites Programmatically • Creating Sites Programmatically • Deleting Sites Programmatically • Securing Sites Programmatically • Adding Role Definitions, Role Assignments, and Role Definition Bindings
Creating Sites Programmatically • Add to an SPWebCollection object • SPWeb.Webs returns an SPWebCollection object SPWebthisWeb = SPContext.Current.Web; SPWebnewWeb = null; if (!thisWeb.Webs["JobData"].Exists) //Existence check { try { newWeb = thisWeb.Webs.Add("JobData", //Web Url "Job Data", //Title "Data for Jobs", //Description 1033, //LocaleID "STS#1", //Template true, //Use unique permissions? false); //Convert if already exists? } catch (Exception ex) { //handle exceptions } finally { newWeb.Dispose(); }
Deleting Sites Programmatically • Deleting a site does not move it to the Recycle Bin • Deleting a Web object directly • Deleting a Web from an SPWebCollection object • Specify a relative Url SPWebthisWeb = SPContext.Current.Web; thisWeb.Webs["JobData"].Delete(); //permanently deletes the site SPWebthisWeb = SPContext.Current.Web; thisWeb.Webs.Delete("JobData"); //permanently deletes the site
Securing Sites Programmatically • By default, sites inherit permissions from their parents • Breaking inheritance • On creation • After creation newWeb = thisWeb.Webs.Add("JobData", "Job Data", "Data for Jobs", 1033, "STS#1", true, //Use unique permissions? false); //Break inheritance, but retain a copy of //the previously inherited role assignments newWeb.BreakRoleInheritance(true); newWeb.Update();
Adding Role Definitions, Role Assignments, and Role Definition Bindings • Multistep process for assigning users to roles //Create a new role assignment SPRoleAssignmentroleAssign = new SPRoleAssignment (@"SHAREPOINT\KrishnaS", "krishnas@sharepoint.com", @"SHAREPOINT\KrishnaS", "HR Manager"); //Retrieve a reference to a role definition SPRoleDefinitionroleDef = newWeb.RoleDefinitions["Contribute"]; //Bind the role definition to the role assignment roleAssign.RoleDefinitionBindings.Add(roleDef); //Add the role assignment to the Web newWeb.RoleAssignments.Add(roleAssign); //Save the Web newWeb.Update();
Lesson 2: Working with SharePoint Lists Programmatically • Creating Lists Programmatically • Deleting Lists Programmatically • Adding and Removing List Fields
Creating Lists Programmatically • Add to an SPLists object • SPWeb.Lists returns an SPListCollection object • SPListCollection.Add returns a GUID • Seven overloads for the SPListCollection.Add() method • Feature ID, Quick Launch options, document template, custom schema //Get a reference to a Web SPWebthisWeb = SPContext.Current.Web; Guid jobDefGuid = thisWeb.Lists.Add( "Job Definitions", //Title "Jobs, Salary Ranges, and Descriptions", //Description SPListTemplateType.GenericList); //List Template SPListjobDef = thisWeb.Lists[jobDefGuid]; //Get list by GUID
Deleting Lists Programmatically • Calling the Delete method • Calling the Recycle method //Get a reference to a Web SPWebthisWeb = SPContext.Current.Web; //Get a reference to a list, by name, GUID, or position SPListjobDef = thisWeb.Lists["Job Definitions"]; jobDef.Delete(); //call Delete method, which returns void //Get a reference to a Web SPWebthisWeb = SPContext.Current.Web; //Get a reference to a list, by name, GUID, or position SPListjobDef = thisWeb.Lists["Job Definitions"]; Guid jobDefGuid = jobDef.Recycle(); //Recycle returns a GUID ... ... Guid[] restoreGuids = new Guid[1]; restoreGuids[0] = jobDefGuid; thisWeb.recycleBin.Restore(restoreGuids);
Adding and Removing List Fields • Add to an SPFieldCollection object • SPList.Fields returns an SPFieldCollection object • Delete a Field object directly • Delete a Field from an SPFieldCollection object ... jobDef.Fields.Add("MinSalary", SPFieldType.Currency, true); jobDef.Fields.Add("MaxSalary", SPFieldType.Currency, true); jobDef.Fields.Add("JobDescription", SPFieldType.Text, false); jobDef.Update(); ... SPFieldfld = jobDef.Fields["MinSalary"]; fld.Delete(); fld.ParentList.Update(); ... jobDef.Fields.Delete("MinSalary"); jobDef.Update();
Lesson 3: Working with List Data Programmatically • Retrieving List Items from a List • Querying List Items with CAML • Querying List Items by Using LINQ to SharePoint • Updating List Items • Adding List Items to Lists • Deleting List Items
Retrieving List Items from a List • Items in a list are SPListItem objects • SPListItem objects are contained in SPListItemCollection objects • You can use the following properties to access an SPListItemCollection object: • SPList.Items • SPList.GetItems() • You can use the following methods to retrieve an individual SPListItem object: • SPList.GetItemById() • SPList.GetItemByUniqueId()
Querying List Items with CAML • Retrieve only those items you need to work with SPWebthisWeb = SPContext.Current.Web; SPListopenPositions = thisWeb.Lists["OpenPositions"]; SPQuery query = new SPQuery(); //Set the ViewFields property of the SPQuery object query.ViewFields = "<FieldRef Name='Title' />" + "<FieldRef Name='Location' />" + "<FieldRef Name='Interviewer' />"; //Set the Query property of the SPQuery object query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" +"Developer</Value></Eq></Where>"; //Use GetItems to only retrieve those items needed SPListItemCollectiondeveloperPositions = openPositions.GetItems(query);
Querying List Items by Using LINQ to SharePoint • Use SPMetal to generate entities file • Add entities file to your SharePoint project in Visual Studio • Use early-bound, type-checked, intuitive LINQ statements HREntitiesDataContexthrDC = new HREntitiesDataContext(http://SharePoint); var jobs = from jobList in hrDC.JobDefinitions orderbyjobList.Title select new { jobList.Title, jobList.MinSalary, jobList.MaxSalary }; foreach (varjobDefinition in jobs) { //Do something with jobDefinition.Title //Do something with jobDefinition.MinSalary //Do something with jobDefinition.MaxSalary }
Updating List Items • Set field values for an existing SPListItem object • Call SPListItem.Update() SPWebthisWeb = SPContext.Current.Web; SPListjobDefinitions = thisWeb.Lists["JobDefinitions"]; SPQuery query = new SPQuery(); query.ViewFields = "<FieldRef Name='MaxSalary' />"; query.Query = "<Where><Contains><FieldRef Name='Title' />" + "<Value Type='Text'>Developer</Value>" + "</Contains></Where>"; SPListItemCollectiondeveloperPositions = jobDefinitions.GetItems(query); foreach(SPListItem dev in developerPositions) { dev["MaxSalary"] = double.Parse(dev["MaxSalary"].ToString()) * 1.1; dev.Update(); }
Adding List Items to Lists • Add a new SPListItem object to an SPListItemCollection object • Set the field values • Call the Update method of the SPListItem SPListjobDef = thisWeb.Lists["JobDefinitions"]; SPListItemnewDef; newDef = jobDef.Items.Add(); newDef["Title"]= "Developer"; newDef["MinSalary"] = "40000"; newDef["MaxSalary"] = "80000"; newDef["JobDescription"] = "SharePoint or other Web Developer"; newDef.Update();
Deleting List Items • To delete a list item, you can use one of the following methods: • SPListItem.Delete() • SPListItem.Recycle() • SPListItemCollection.Delete(int index) • SPListItemCollection.DeleteItemById(int id)
Lab: Creating and Manipulating Server-Side Objects • Exercise 1: Creating and Securing Sites Programmatically • Exercise 2: Creating Lists Programmatically • Exercise 3: Retrieving Secured Data Logon information Estimated time: 60minutes
Lab Review • How did you create sites? • How did you secure sites? • How did you retrieve data from secured sites for use in connected Web Parts?
Module Review • In this module, you have learned to: • Work with SharePoint sites programmatically • Work with SharePoint lists programmatically • Work with list data programmatically