Module 2 - Code Snippets: What Developers Need to Know About SharePoint 2010
The following code shows two event handlers:
- The first one (FieldAdded) updates existing list items when a field is added to a list.
- The second handler (ListDeleting) prevents the deletion of a list.
namespace ProductMarketing.ProductTaskEvents
{
public class ProductTaskEvents : SPListEventReceiver
{
public override void FieldAdded(SPListEventProperties properties)
{
SPField newField = properties.Field;
Guid fieldID = newField.Id;
if (newField.Type == SPFieldType.Text)
{
SPList thisList = properties.List;
foreach (SPListItem item in thisList.Items)
{
item[fieldID] = "Please update this existing item...";
item.Update();
}
}
base.FieldAdded(properties);
}
public override void ListDeleting(SPListEventProperties properties)
{
properties.ErrorMessage = "This list is critical to the marketing campaign and cannot be deleted.";
properties.Cancel = true;
}
}
}
The following event handler shows how to update properties of a SharePoint Web site when a new Web is created
namespace ProductMarketing.ProductMarketingWebEvents
{
public class ProductMarketingWebEvents : SPWebEventReceiver
{
public override void WebProvisioned(SPWebEventProperties properties)
{
SPWeb thisNewWeb = properties.Web;
thisNewWeb.AllowUnsafeUpdates = true;
thisNewWeb.Title = "Updated in Code";
thisNewWeb.Update();
thisNewWeb.AllowUnsafeUpdates = false;
base.WebProvisioned(properties);
}
}
}
The following code shows how to enumerate the alerts in a SharePoint site, and also how to enable or disable alerts.
Note: This code is designed to run on an application page that includes:
- An ASP.NET Label control called alertCount
- An ASP.NET button that includes a Click event handler set to the EnableAll funtion
- An ASP.NET button that includes a Click event handler set to the DisableAll funtion
namespace Alerter.Layouts.Alerter
{
public partial class Alerts : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
int enabled = 0;
int disabled = 0;
SPWeb thisWeb = SPContext.Current.Web;
foreach (SPAlert alrt in thisWeb.Alerts)
{
if (alrt.Status == SPAlertStatus.On)
{
enabled++;
}
if (alrt.Status == SPAlertStatus.Off)
{
disabled++;
}
}
alertCount.Text = "Enabled Alerts: " + enabled.ToString()
+ "\nDisabled Alerts" + disabled.ToString();
}
protected void EnableAll(object sender, EventArgs e)
{
SPWeb thisWeb = SPContext.Current.Web;
foreach (SPAlert alrt in thisWeb.Alerts)
{
alrt.Status = SPAlertStatus.On;
alrt.Update();
}
}
protected void DisableAll(object sender, EventArgs e)
{
SPWeb thisWeb = SPContext.Current.Web;
foreach (SPAlert alrt in thisWeb.Alerts)
{
alrt.Status = SPAlertStatus.Off;
alrt.Update();
}
}
}
}
The following code creates a new SharePoint site (SPWeb object):
SPSite thisSite = SPContext.Current.Site;
thisSite.RootWeb.AllowUnsafeUpdates = true;
SPWeb hrWeb = thisSite.RootWeb.Webs.Add("HRWeb",
"Human Resources Web",
"For use by HR Employees",
1033,
"STS#0",
false,
false);
hrWeb.Dispose();
thisSite.RootWeb.AllowUnsafeUpdates = false;
The following code enumerates all of the services provided by the current farm's SPFarm object and displays them in a treeview control in an application page.
Note: The code relies on there being a tree-view named farmContents in the application page.
namespace Hierarchy.Layouts.Hierarchy
{
public partial class HierarchyViewer : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
SPFarm thisFarm = SPFarm.Local;
TreeNode node;
farmContent s.Nodes.Clear();
foreach(SPService svc in thisFarm.Services)
{
node = new TreeNode();
node.Text = "(Type=" + svc.TypeName + ") " + svc.DisplayName;
farmContents.Nodes.Add(node);
}
farmContents.CollapeAll();
}
}
The following code enumerates all of the services, web applications, site collections, sites, and lists in a SharePoint farm, and displays them in a treeview control in an application page.
Note: The code relies on there being a tree-view named farmContents in the application page. Also note how the addWebs() function is called recursively to ensure sites and subsites are enumerated and displyed in the treeview.
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Administration;
namespace Hierarchy.Layouts.Hierarchy
{
public partial class HierarchyViewer : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
SPFarm thisFarm = SPFarm.Local;
TreeNode node;
farmContents .Nodes.Clear();
foreach(SPService svc in thisFarm.Services)
{
node = new TreeNode();
node.Text = "(Type=" + svc.TypeName + ") " + svc.DisplayName;
farmContents.Nodes.Add(node);
TreeNode svcNode = node;
if (svc is SPWebService)
{
SPWebService webSvc = (SPWebService)svc;
foreach (SPWebApplication webApp in webSvc.WebApplications)
{
node = new TreeNode();
node.Text = webApp.DisplayName;
svcNode.ChildNodes.Add(node);
TreeNode webAppNode = node;
foreach (SPSite site in webApp.Sites)
{
try {
node = new TreeNode();
node.Text = site.Url;
webAppNode.ChildNodes.Add(node);
TreeNode siteNode = node;
site.CatchAccessDeniedException = false;
try
{
node = new TreeNode(site.RootWeb.Title, null, null, site.RootWeb.Url, "_self");
siteNode.ChildNodes.Add(node);
TreeNode parentNode = node;
foreach (SPList list in site.RootWeb.Lists)
{
node = new TreeNode(list.Title, null, null, list.DefaultViewUrl, "_self");
parentNode.ChildNodes.Add(node);
}
foreach (SPWeb childWeb in site.RootWeb.Webs)
{
try {
addWebs(childWeb, parentNode);
} finally
{
childWeb.Dispose();
}
}
}
catch
{ }
}
finally
{
site.Dispose();
}
}
}
}
}
farmContents.CollapseAll();
}
void addWebs(SPWeb web, TreeNode parentNode)
{
TreeNode node;
node = new TreeNode(web.Title, null, null, web.Url, "_self");
parentNode.ChildNodes.Add(node);
parentNode = node;
foreach (SPList list in web.Lists)
{
node = new TreeNode(list.Title, null, null, list.DefaultViewUrl, "_self");
parentNode.ChildNodes.Add(node);
}
foreach (SPWeb childWeb in web.Webs)
{
try {
addWebs(childWeb, parentNode);
} finally {
childWeb.Dispose();
}
}
}
}
}