Skip to main content

Cloud

Working with documents stored in SharePoint document library programmatically

On a recent project, I had to programmatically work with the documents stored in SharePoint document library. Having done programming access to lists before, I was expecting something very similar to the way SharePoint lists store attachments. This expectation however did not turn to be true.

Programmatic access to the documents in the document library is very different from access to attachments to a list. I thought it might be useful to publish code snippets for such access:

Inserting a document into a doc library

public void SPInsertFile(SPWeb web, string filename, byte[] file)
{
string SPDocLibName = "Doc Lib Name";
web.AllowUnsafeUpdates = true;
SPFolder docLib = web.GetFolder(SPDocLibName);
if (docLib.Exists != true) throw new Exception("Unable to find ‘" + SPDocLibName + "’ document library in the current SPWeb");
if (file == null) throw new Exception("Unable to insert an empty file");
SPFile spFile = docLib.Files.Add(filename, file);
}

Deleting a document from a doc library

static public void SPDeleteFile(SPWeb web, string filename)
{
string SPDocLibName = "Doc Lib Name";
web.AllowUnsafeUpdates = true;
SPFolder docLib = web.GetFolder(SPDocLibName);
string filePath = SPDocLibName + "/" + filename;
SPFile spFile = web.GetFile(filePath);
if (spFile.Exists != true) throw new Exception("Unable to find ‘" + filePath + "’ document in the current SPWeb");
spFile.Delete();
}

Updating a document in the doc library

static public void SPUpdateFile(SPWeb web, string oldFilename, string newFilename, byte[] file)
{
string SPDocLibName = "Doc Lib Name";
web.AllowUnsafeUpdates = true;
SPFolder docLib = web.GetFolder(SPDocLibName);
string oldFilePath = SPDocLibName + "/" + oldFilename;
SPFile spFile = web.GetFile(oldFilePath);
if (spFile.Exists != true) throw new Exception("Unable to find ‘" + oldFilePath + "’ document in the current SPWeb");
if (file == null) throw new Exception("Unable to save an empty file");

if (oldFilename == newFilename) //if the same file extension, update.
{
spFile.SaveBinary(file, true);
}
else //otherwise, try to insert and then delete
{
SPInsertFile(web.Site.RootWeb, newFilename, file);
spFile.Delete();
}
}

Notice that the filename in the document library is used to identify the document. Therefore, if the document needs to be renamed, it requires deleting it and reinserting it. Since such deleting and inserting is an atomic operation it should ideally be part of a transaction. Unfortunately transaction support is non-existent in SharePoint. To mitigate potential problems, notice how all the pre-work is done prior to the actual insert/delete. Also notice that we first insert, then delete. This way at worst we may end up with two files but we never delete a file unintentionally.

As always, if you see any mistakes or inaccuracies in this blog, please let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

PointBridge Blogs

More from this Author

Follow Us