Programmatically Add a File in SharePoint


Yet another MSDN code sample 🙂 This one is for how to programmaitcally copy and/or add a file to a SharePoint library. Everything is commented in line.

//open target site collection
using(SPSite site = new SPSite(“vendorurl”))
{
//open target web
using(SPWeb vendorWeb = site.OpenWeb(“VendorWeb -RelativeURL”))
{
vendorWeb.AllowUnsafeUpdates = true;
//get folder of root of the target document library
SPFolder targetFolder = web.Lists(“targetLibraryName”).Folder();
//get the file collection to add our file in to
SPFileCollection _files = targetFolder.Files;
//get the byte array of the source item to send to the library
byte[] fileToCopy = sourceItem.File.OpenBinary();
//construct the proper url – usually this is handed off to a function that returns a string
string listurl = targetList.DefaultViewUrl;
if (listurl.Contains(“/Forms/”) == true)
{
int index = listurl.IndexOf(“/Forms/”);
listurl = listurl.Remove(index);
}
else if (listurl.Contains(“.aspx”) == true)
{
int index = listurl.LastIndexOf(“/”);
listurl = listurl.Remove(index);
}
//add our file to sharepoint, the sourceItem.Properties copies metadata when copying from an existing sharepoint file, the true at the end is for overwriting the existing copy of the file if any
SPFile file = _files.Add(listurl + “/” + item.File.Name.ToString(), fileToCopy, sourceItem.Properties, true);
//get the splistitem of the uploaded file
SPListItem finalItem = file.Item;
//update a field in the uploaded file
finalItem[“originatingDocumentId”] = sourceItem[“DocumentID”].ToString();
//update the list item
finalItem.Update();
//finish up 🙂
vendorWeb.AllowUnsafeUpdates = false;
}
}
  1. #1 by Gary Newman on March 4, 2011 - 3:10 pm

    Hey nice job on the web site, your code samples look real good. fpweb.net offers free web sites to 2010 bloggers, that is what I’m using with 2010 now. I was just wondering why you dont post your name on MSDN or here.
    Gary Newman

    • #2 by Maarten on March 4, 2011 - 7:36 pm

      Thank you!

      My name is posted, it’s my username 🙂

      • #3 by Gary Newman on March 18, 2011 - 3:46 am

        I was just talking with some of your people at Slalom consulting while I was Dallas SharePointFest. You going to be at Denver or Chicago SharePointFest?

        • #4 by Maarten on March 18, 2011 - 7:18 am

          I’m out of the San Francisco office so won’t be going to either unfortunately.

          • #5 by Gary Newman on March 24, 2011 - 11:04 am

            Marten, have you ever or thought about submitting for your MVP?

          • #6 by Maarten on March 24, 2011 - 11:26 am

            Would love to get MVP. Do they allow self-submit now? I thought you had to be recommended by a Microsoft employee or an existing MVP.

  2. #7 by Gary Newman on March 25, 2011 - 9:47 pm

    Marten,
    Yes you can submit yourself. I would get to know the MS People in your area. Take them out to lunch talk about MS. Here is the link to submit yourself.
    http://mvp.support.microsoft.com/gp/mvpnominate

    • #8 by Maarten on March 28, 2011 - 10:44 am

      Thanks! I went ahead and nominated myself. Let me know if you’d like a nomination or would be willing to nominate me as well.

      • #9 by Gary Newman on March 28, 2011 - 11:13 am

        Do you have a email address so this does not have to be so open. Or just email me.

      • #10 by Gary Newman on March 28, 2011 - 11:14 am

        I would be happy to, did you get the format correct?

(will not be published)


%d bloggers like this: