Ads

Saturday, 23 March 2013

C# code to add document to a document library in SharePoint 2010 Programatically

 using (SPSite objSite = new SPSite("urlAddress"))
 {
        objSite.AllowUnsafeUpdates = true;
         using (SPWeb oSPWeb = objSite.OpenWeb())
         {
                oSPWeb.AllowUnsafeUpdates = true;
  
               //Test1 is the document library
                SPList docLibrary = oSPWeb.Lists["Test1"];
                 using (FileStream fs = File.OpenRead(FullPath))
                 {
                           //Name is the name of the file
                          SPFile file = oSPWeb.Files.Add(oSPWeb.Url.ToString() + "/" + docLibrary.Title.ToString() + "/" + Name, fs);
                          file.Update();
                  }
                  docLibrary.Update();
                  oSPWeb.AllowUnsafeUpdates = false;
          }
          objSite.AllowUnsafeUpdates = false;
                  
 }
 

No comments:

Post a Comment

Ads