I want to create a web part to place on a document set web part. How would I get the context of the document set and the documents within the set from within the web part?


3 Answers 3


The url for the document set welcome page includes an ID in the query string, so you can use this parameter to find the document set:


var id = Convert.ToInt32(HttpContext.Current.Request.QueryString["ID"]);
SPListItem item = SPContext.Current.Web.Lists["MyDocumentLibrary"].GetItemById(id);
var dsItem = Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.GetDocumentSet(item.Folder);

NOTE: You will need to add a reference to Microsoft.Office.DocumentManagement

  • Do you know how I can determine the library name knowing only the id of the item? i.e. id
    – David
    Jun 9, 2011 at 19:44
  • In addition to the ID parameter, the welcome page also includes these query string parameters: FolderCTID (the content type), List (the List ID). So you can get the listId = ...QueryString["List"] and then: SPContext.Current.Web.Lists[new Guid(listId)]
    – Laurie
    Jun 9, 2011 at 20:22
  • If your document set is in a sub folder (not at the root of the document library) you have to specify the RootFolder parameter. E.g. server/MyDocumentLibrary/Forms/MyDocumentSet/… Sep 29, 2014 at 20:46
var item = SPContext.Current.ListItem;

will get you the ListItem, then

var folder = item.folder;

will get you the folder matching the item, and

var docset = DocumentSet.GetDocumentSet(folder);

will get you the document set for that folder. Though most things you might want to do (like getting/setting fields) really only need the listitem or maybe the folder.


http://office.microsoft.com/en-us/sharepoint-server-help/customize-the-welcome-page-for-a-document-set-HA101782474.aspx says "The Document Set Welcome page is a Web Part page that can be edited just as you would edit any Web Part page". Any further questions?

  • 1
    I understand that the welcome page is a web part page. This doesn't answer the question. If I put a web part on the welcome page (custom web part), programmatically how do I get a context to the current document set within the web part. I want to perform operations on the documents within the current document set and to do this i need to get some sort handle the current set. thx
    – David
    Jun 5, 2011 at 11:15
  • 1
    This wasn't quite clear from how you formulated the question. Please edit the question to make it more specific. The question title is "how to add a webpart" while in the body you ask another question "how to get the context of the displayed document set". Jun 6, 2011 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.