SharePoint Code


13
Dec 09

Quickly searching through a SharePoint List

Background

The project I’m currently working on involves searching through a lot of data as quickly as possible.

There are documents around the Internet that detail the fastest returns for querying a SharePoint list, aside from SQL queries which aren’t supported, the next best way is SPQuery.

1
2
3
4
SPQuery query = new SPQuery();
 
query.Query = BuildQuery();
SPListItemCollection items = list.GetItems(query);

The BuildQuery() method is a place-holder for another project I’m working. The intention will be able to create CAML queries on the fly. I’ve done this for the current project but it’s not really ready to talk about here.


12
Dec 09

Programmatically set the Approval Status of a SharePoint ListItem

Background

I’m working on a project at the moment where the approval of a list item needs to be by a specific person based on the content of the item. Normally this would be a great opportunity to use some Workflow but for other reasons this was decided against.

This particular project is involving a lot of web service work due to the Infopath component, to this end it seemed prudent to use the Web Service to perform the approval action.

Approval Status

The approval status of List Item is held in the ModerationInformation property of the SPListItem object.

1
2
3
4
5
6
7
8
using (SPWeb web = listItem.Web())
{
      bool allowUpdates = web.AllowUnsafeUpdates;
      web.AllowUnsafeUpdates = true;
      listItem.ModerationInformation.Status = SPModerationStatusType.Approved;
      listItem.Update();
      web.AllowUnsafeUpdates = allowUpdates;
}

So updating the Approval is actually very easy. The options for SPModerationStatusType can be found on the following MSDN page for SPModerationStatusType.


16
Oct 09

SharePoint Site Library Size

Background

I was recently asked to supply a breakdown of the Document Library sizes within a SharePoint Site Collection (SPSite). I had a look through the SharePoint API and couldn’t find any obvious information on the SPDocumentLibrary or SPList objects.

The first solution I came up with was to use some recursion to enumerate through each SPWeb in the Site Collection and then enumerate through each Document Library in the web, adding the file sizes in each library as I went. This approach worked but took a huge amount of time.

At previous clients, it would have been acceptable to dive into the Content Database for the site collection and write a query to do the work, unfortunately at the current site there are decent policies and rules in place to stop this from being done.

SPSite.StorageManagementInformation

By chance, I found a method on the SPSite object called StorageManagementInformation

1
2
3
4
5
6
7
8
using (SPSite site = new SPSite(url))
{
       DataTable storageInfo;
       storageInfo = site.StorageManagementInformation(
       SPSite.StorageManagementInformationType.DocumentLibrary,
       SPSite.StorageManagementSortOrder.Decreasing,
       SPSite.StorageManagementSortedOn.Size, 2000);
}

From here the data table can be queried as normal to output the information about each Document Library;

1
2
3
4
5
6
7
8
9
foreach (DataRow row in storageInfo.Rows)
{
      double size = Convert.ToDouble((row["Size"].ToString()) / 1024) / 1024;
      Console.WriteLine(string.Format("{0}/{1}\t{2}\t{3}",
                                       site.Url,
                                       row["WebUrl"].ToString(),
                                       row["Title"].ToString(),
                                       size));
}

More Information

There are several options for StorageManagementInformationType enum. Its worth noting that the row columns will be slightly different for each of them.