SharePoint API and Development in ASP.Net

Download Report

Transcript SharePoint API and Development in ASP.Net

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
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
ASP.Net Website with SharePoint Object Model
Create ASP.NET website in the following url:
http://<server_name>/_layouts/<Web_Application_Name>
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
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Demo: “Hello World” ASP.NET Application
Demo: “HelloWorld” 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)
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
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Getting the Current Website
using Microsoft.SharePoint;
SPWeb web = SPContext.Current.Web;
labelMessage.Text = web.Title;
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);
}
}
}
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);
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();
}
}
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Demo: Site Organization Classes
Demo: Site Organization Classes
1. Use SPContext, SPSite, SPWeb, SPWebCollection,
SPListCollection, and SPList classes
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Lab: Exercises 1-2
Perform exercises 1 - 2 from Basic SharePoint API
Development Lab
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
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];
}
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);
}
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();
}
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();
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Demo: List Classes
Demo: List Classes
1. Use SPList, SPField, SPView, and SPListItem classes
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Lab: Exercises 3 - 5
Perform exercises 3 - 5 from Basic SharePoint API
Development Lab
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)
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
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<br>");
}
else
{
Response.Write(web.Title + " inherits
permissions<br>");
}
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("<b>All domain user principals in
this site: " + "</b><br>");
foreach (SPUser user in users)
{
Response.Write(user.Name + "<br>");
}
// All domain users and groups within the site
collection
users = web.SiteUsers;
Response.Write("<b>All users in this site
collection: " + "</b><br>");
foreach (SPUser user in users)
{
Response.Write(user.Name + "<br>");
}
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("<b>All SharePoint groups in this
site: " + "</b><br>");
foreach (SPGroup group in groups)
{
Response.Write(group.Name + "<br>");
}
// All SharePoint Groups in this site collection
groups = web.SiteGroups;
Response.Write("<b>All SharePoint groups in this
site collection: " + "</b><br>");
foreach (SPGroup group in groups)
{
Response.Write(group.Name + "<br>");
}
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
SharePoint Group Users
// All users in "Students" SharePoint group
users = web.Groups["Students"].Users;
Response.Write("<b>All users in \"Students\"
SharePoint group: " + "</b><br>");
foreach (SPUser user in users)
{
Response.Write(user.Name + "<br>");
}
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("<b>All role definitions in this site: " +
"</b><br>");
foreach (SPRoleDefinition roleDefinition in
roleDefinitions)
{
Response.Write(roleDefinition.Name + "<br>");
}
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Role Assignments For the Site
SPRoleAssignmentCollection webRoleAssignments =
web.RoleAssignments;
Response.Write("<b>All role assignments in this
site: " + "</b><br>");
foreach (SPRoleAssignment webRoleAssignment in
webRoleAssignments)
{
Response.Write(webRoleAssignment.Member.Name +
" "
+
webRoleAssignment.RoleDefinitionBindings[0].Name +
" "
+ webRoleAssignment.Parent.ToString() +
"<br>");
}
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");
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);
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Demo: Security Classes
Demo: Security Classes
1. Use SPGroup, SPUser, SPRoleAssignment, and
SPRoleDefinition classes
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
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
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);
}
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();
}
}
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Demo: Administration Classes
Demo: Administration Classes
1. Use SPWebService, SPFarm, SPWebApplication, and
SPContentDatabase classes
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 +
"<br>");
Response.Write("state is " + up["State"].Value);
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Demo: User Profile Classes
Demo: User Profile Classes
1. Use UserProfileManager and UserProfile classes from
Microsoft.Office.Server.UserProfiles namespaces
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.
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.
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 “<script>alert();</script>”
• Response.Write(list.Title) would cause a message box
to pop up.
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Application infrastructure
•
Web.config for storing properties
•
Localizations using .NET standards
•
Windows installer or xcopy deployment
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.
Demo: Examining Object Model Using the SDK
Demo: Examining Object Model Using the SDK
1. WSS object model reference
2. Office object model reference
Copyright © 2006 Pilothouse Consulting Inc. All rights reserved.