Monday, October 25, 2010

Programmatically adding documents to a document set using SharePoint 2010

Programmatically adding documents to a document set using SharePoint 2010.

In continuation with the previous post about how to create a document sets programmatically,
here i will post a little code snippet for adding a document to this document set that we just
created in our previous post.
For this example i am adding a file to our custom document set which already exists on
SharePoint server. You can however, use the file upload control to browse and add a file to your
docuemntset.

void addDocumentToDocuemntSet()
{

// Get the existing file
SPFile spfile = web.GetFile(“http://SPSite:8080/Lists/DemoLib/abc.txt”);

byte[] documentBytes = new byte[spfile.TotalLength];

if (spfile.Exists)
{
FileStream fs = new FileInfo(spfile.url).OpenRead()

fs.Read(documentBytes, 0, (int)fi.Length);

//add the default document to your document set template

newDocumentSetTemplate.DefaultDocuments.Add(spfile.Name,
web.ContentTypes["Document"].Id, documentBytes);

}

No comments:

Post a Comment