Register sites in Sharepoint Site Directory programmatically

As part of my ongoing quest to autogenerate sites I need to register created sites in the site directory in code. The following would apply for any generated WSS site or other, potentially external, source.

When using the regular "Create Site" link from the Site Directory area you are actually passed through three operations:

  1. Create the actual WSS top-level website.
  2. Add an item to the standard list in the site directory.
  3. Register the site for search and crawling.

When doing the same operation in code, my ambition was to accomplish the entire task through the Sharepoint Web Services API. The first task is actually easy to accomplish; just call the CreateSite method of the Admin webservice. The second task is a bit more cumbersome because you have to construct some nasty CAML and pass it to the UpdateListItems method of the Lists web service. The third operation is not supported through the web services API (please correct me if I'm wrong).

I decided to perform at least step 2 and 3 using the object model and expose the operation as a custom web service.

In step 2 you have to make sure you're actually accessing the proper names for the fields in the list. For example the standard Title and URL fields that you probably want to set is called "SiteTitle" and "SiteURL". Impossible to notice without actually dissecting the XML or browsing the SPListItem object in the QuickWatch. I support custom fields by accepting a name/value collection in the webservice and setting the SPListItem field values in a loop. In the sample code it's a hashtable. The principle is to put the responsibility of knowing the schema of the list in question on the caller.

Your goal in step 3 is to make your site appear in the list under Portal > Site Settings > Configure search and indexing > View sites that are indexed. (Approved Sites). This is actually a completely separate operation and notice that there are no connection between adding your site to the Site Directory list and adding it to the search catalog. The key method is AreaManager.SuggestDeepCrawl which enlists your site for crawling. If you are registering WSS sites make sure you're using the same root URL as search is configured for. Using http://localhost instead of http://servername will not index your content. The gatherer log will simply state that "The address was excluded because crawl depth restrictions were exceeded" (let me know if you've figured out where to adjust crawl depth restrictions).

I have published code to perform step 2 and 3 here. I tested this code in a WinForms app on the SPS server and it registers an existing site in the site directory listing with metadata, and also enlists the url for search so that the content of the site becomes searchable through Sharepoint.


 

2 Comments

  • Thanks for this! I am very new to sharepoint and I would like to create new site based on entries in a list. For example, if I have a projects list and I enter a new item in that list, I want a site to be automatically created for me. I guess I need to create an ItemAdded event handler for this list to perform this function, but alas I am not a programmer and can't figure how to do this! Any ideas?

    Brian Cowley
    bcowley@netsysconsulting.ie

  • Thank, great code.
    Cheers

Comments have been disabled for this content.