Sunday, March 8, 2009

Archieving documents

Scenario:
You want to Achieving documents from sharepoint document library.

Solution:
I build an UI for the client with the following code behind

Code:

int itemId =0 ; 

SPWeb web = SPContext.Current.Web;

SPListItem selItem = (SPListItem)web.Lists["Shared Documents"].Items[itemId];

SPFile doc = selItem.File;
byte[] buffer = doc.OpenBinary();
string path = @"C:\Archieve\SharedDocuments" + doc.Name;

FileStream fs = new FileStream(path,FileMode.Create);
fs.Write(buffer,0,buffer.Length);
fs.Flush();
fs.Close();

SPDocumentLibrary docLib = (SPDocumentLibrary)selItem.ParentList;
docLib.Items.DeleteItemById(itemId);
Response.WriteLine("Item has now been archived!");

1 comments:

Anonymous,  April 7, 2009 at 8:09 AM  

That's a lot of different ways to spell "archive". ;-)