Monday, August 3, 2009

C# Code to Update Document in SharePoint Document library

SPSite mySite =null;
SPWeb myWeb =null;

/* represents the current site */

using (mySite = SPContext.Current.Site)
{
using (myWeb = mySite.OpenWeb())
{

/* Modifes Windows SharePoint Services data in some way, you may need to allow unsafe updates on the Web site, without requiring a security validation. */

myWeb.AllowUnsafeUpdates = true;
Guid listId = new Guid(listID);
SPList documentLibrary = myWeb.Lists[listId];
SPListItem document = documentLibrary.GetItemById(itemID);

SPFolder oFolder = documentLibrary.RootFolder;

SPFileCollection collFiles = oFolder.Files;
SPFile oFile = null;

/* overwrite the file if exists in the document library */

oFile = collFiles.Add(document.Url, bytes, true);

SPListItem listItem = oFile.Item;

listItem["Col1"] = Value1;
listItem["Col2"] = Value2;

/* updates the versionsing of the document library also*/
listItem.UpdateOverwriteVersion();

myWeb.AllowUnsafeUpdates = false;
myWeb.Dispose();

}
}

1 comment:

digital certificate said...

very helpful article, i was always curious about all these complex algorithms that are being used in these ssl encryptions.