Windows SharePoint Services (WSS) is the Microsoft document collaboration standard. As many organizations do, Microsoft leverages SharePoint as the platform for its portal site as well. Lured by SharePoint’s simple site configuration and intuitive interface, many companies have placed their human resources and financial reporting information on multiple SharePoint sites for anyone in their organizations to access.
With SharePoint, building Web sites is now a possibility for any user, and users have taken advantage of this capability in a big way. This has brought about a new dilemma: how to manage all of the information on tens or hundreds of SharePoint sites. Making users aware of new or changed content is a good first step.
SharePoint does have subscription features for notifying users when changes occur. The notifications require active intervention by the user, and they send a separate email for each change. How do you make users aware without inundating them with email every time a document on the site changes? Oftentimes, a user simply requires a periodic summary of the changes.
This article demonstrates how you can leverage the SharePoint SDK classes to build a tool that collects all the changes to a site. (The tool is called a Newsletter Generator because it presents the collected changes in a Newsletter format.) It begins with a summary of the application and then discusses its implementation.
The Newsletter ApplicationThis example uses the SharePoint SDK to implement the demonstration as an ASP.NET application. An ASP.NET solution is appropriate for the following reasons:
- A Web Part would have taken longer to develop and given the requirements no advantage over the ASP.NET application.
- A desktop application would’ve required the Web Services API or access to the server. For performance and broader capabilities, the example uses the Microsoft.SharePoint assembly. Also, the Newsletter application users are not server administrators, and giving anyone but an administrator direct access to the server is not a best practice.
Figure 1 shows a screen capture of the Newsletter application.
Figure 1: The Newsletter ApplicationThe application sends newsletters at monthly intervals. To send only the changes that occurred since a point in time, the application allows the user to select the starting point. The application also has options to include new documents (Date Created), changed documents (Date Modified), or both (changed and created).
Newsletter also allows the user to start on any site on the SharePoint server. Recurse instructs the application to include subsites of the current site.
Newsletter information in HTML format is generated at the bottom of the application. Generated information can be edited and emailed.
It’s time to look at some of the changes to SharePoint that the solution’s implementation requires.
SharePoint ChangesIn a typical newsletter, each news item contains a title followed by a blurb that summarizes the content. This newsletter demonstration ignores some documents. To implement both requirements, some additional properties must be added to the documents contained in SharePoint.
The “Add Columns” feature inside the “Modify Settings and Columns” option in the Document library allows adding or removing document properties. Figure 2 shows the properties added to the Documents that this example includes in the newsletter.
Figure 2: Properties Added to the DocumentsA dropdown menu on the document allows you to modify properties after a document has been added to the Library (see Figure 3).
Figure 3: Dropdown Menu to Modify PropertiesWith SharePoint configured to support the newsletter demonstration, the remaining task is to build the Newsletter program.
The SharePoint SDK ClassesSharePoint is logically partitioned in a hierarchal fashion. At the top of the hierarchy are Virtual Servers. Virtual Servers handle things like authentication configuration and IIS Application Pool. Each Virtual Server contains groups of Web sites called Site Collections. Site Collections allow you to manage multiple SharePoint Web sites.
Keep in mind that Site Collection refers to the SharePoint Site Collection and not a collection object you normally access in the .NET Framework.
The following three classes are critical for navigating SharePoint and manipulating files in SharePoint:
- SPSite provides access to SharePoint Site Collections and allows you to navigate between SharePoint sites.
- SPWeb provides methods for accessing objects in a given site.
- SPFolder allows you to manipulate documents in a document library or list.
In a moment you’ll dive into some C# code, but first review how to use the class objects to access SharePoint. To access the Web site containing the Site Collection, along with all the document libraries on the site, start by opening a SPSite class using the URL for the site. SPSite includes an OpenWeb() method for accessing the site containing the collection. OpenWeb() returns a SPWeb object. SPWeb contains a folders collection for accessing document libraries.
You can access the SharePoint classes in two ways: a Web service and a .NET assembly (Microsoft.SharePoint). This example opts for the .NET assembly because of its increased speed and functionality.
Now that you’re familiar with the SharePoint objects, you can move on to the implementation details.
Applying the SharePoint SDKThe GetWebDetails function utilizes the classes discussed in the previous section to generate the newsletter. The following snippet of code is from the GetWebDetails function:
private void GetWebDetails(SPWeb web)
// enumerate over all the folders on the web
foreach(SPFolder folder in web.Folders)
// Do the same for all subwebs
foreach(SPWeb subWeb in web.Webs)
GetWebDetails first collects the newsletter information from the root site. Next, if the “Recurse” option has been selected, the application proceeds to visit all of the subsites and the subsites of the subsites. The subroutine GetWebDetails is invoked continuously until all subsites have been visited.
You’re probably wondering why the example doesn’t utilize the AllWebs collection on the SPWeb class. The recursive functionality allows you to more easily augment the program later. For example, adding a “depth” option would restrict the function to visit only a certain number of levels of subsites.
Security ConsiderationsWhen you set up the example, you must remember to configure proper security. The demonstration opted for an ASP.NET Web site instead of a Web Part, which had some security ramifications. For instance, you would access the SharePoint sites using the account set up with the Application Pool assigned to the ASP.NET Newsletter application. Be sure the account has proper access to the SharePoint sites you will be accessing.
Other options are available for configuring the ASP.NET application. Refer to the SharePoint SDK for more information on impersonation and the other security options.
Extending the NewsletterConsider the following suggestions for extending the demonstration in this article:
- Iterate over all of the Web sites on all of the Virtual Servers using the SPGlobalAdmin class and the SPVirtualServerCollection.
- The application does not navigate into subfolders. You can modify the application to access subfolders using the SubFolders collection in the SPFolder class.
- It would not take much to extend the application to deliver the email message to groups of email addresses. You could even collect the addresses from the users on the site and deliver an email to each user. Collecting user information from SharePoint is straightforward. The SPWeb class contains a collection called “Users”. The Users collection contains SPUser classes. SPUser contains a property called Email.
- Using techniques you learned from the Newsletter example, you could build a custom site map for your SharePoint sites.
You’ll find other helpful resources on the Web. “SharePoint Products and Technologies Web Component Directory” on the Microsoft Web site contains a number of well documented code samples.
Keep Your SharePoint Users UpdatedUsing the SPWeb, SPSite, and SPFolder classes built into the SharePoint SDK, you can build a newsletter application to keep your users abreast of changes to your SharePoint sites.
Download the Code
To download the accompanying source code for the example, click here.
About the Author
Jeffrey Juday is a software developer with Crowe Chizek in South Bend, Indiana. He has been developing software with Microsoft tools for more than 12 years in a variety of industries. Jeff currently builds solutions using BizTalk 2004, ASP.NET, SharePoint, and SQL Server 2000. You can reach Jeff at [email protected].