Build a SharePoint Newsletter Generator That Alerts Users to Changes

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 e-mail for each change. How do you make users aware without inundating them with e-mail 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 Application

This 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 Application

The 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 e-mailed.

It's time to look at some of the changes to SharePoint that the solution's implementation requires.

SharePoint Changes

In 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 Documents

A 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 Properties

With SharePoint configured to support the newsletter demonstration, the remaining task is to build the Newsletter program.

The SharePoint SDK Classes

SharePoint is logically partitioned in a hierarchal fashion. At the top of the hierarchy are Virtual Servers. Virtual Servers handle things like authentication configuration and the 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 SDK

The 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)
   {
      GetFolderDetails(folder);
   }

   // Do the same for all subwebs
   if(m_RecurseWebs)
   {
      foreach(SPWeb subWeb in web.Webs)
      {
         GetWebDetails(subweb);
      }
   }

}

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 Considerations

When 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 Newsletter

Consider 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 by using the SubFolders collection in the SPFolder class.
  • It would not take much to extend the application to deliver the e-mail message to groups of e-mail addresses. You could even collect the addresses from the users on the site and deliver an e-mail 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.
  • By 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 Updated

By using 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.



About the Author

Jeffrey Juday

Jeff is a software developer specializing in enterprise application integration solutions utilizing BizTalk, SharePoint, WCF, WF, and SQL Server. Jeff has been developing software with Microsoft tools for more than 15 years in a variety of industries including: military, manufacturing, financial services, management consulting, and computer security. Jeff is a Microsoft BizTalk MVP. Jeff spends his spare time with his wife Sherrill and daughter Alexandra.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds