SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question
up vote 4 down vote accepted

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

share|improve this answer
Do you know how I can determine the library name knowing only the id of the item? i.e. id – David Jun 9 '11 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 '11 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/… – Bernd Rickenberg Sep 29 '14 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.

share|improve this answer 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?

share|improve this answer
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 '11 at 11:15
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". – Alexey Krasheninnikov Jun 6 '11 at 9:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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