Programmatically upload documents using SharePoint 2010 Client Object Model

The following code snippet helps us to programmatically upload documents to document library, using Client Object Model.

 using (ClientContext clientContext = new ClientContext("http://yoursitecollection"))
            {

                Web oWeb = clientContext.Web;
                List oList = oWeb.Lists.GetByTitle("Shared Documents");               

                FileCreationInformation oFileCreationInformation = new FileCreationInformation();                  
                
                byte[] filecontent=System.IO.File.ReadAllBytes(@"C:SundarTestFile.txt");
                oFileCreationInformation.Content = filecontent;
                oFileCreationInformation.Url = @"http://yoursitecollection/Shared Documents/TestFile.txt";              


                oList.RootFolder.Files.Add(oFileCreationInformation);
                

                clientContext.Load(oList);
                clientContext.ExecuteQuery();        
                
                
            }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 Subscribe to my blog

One thought on “Programmatically upload documents using SharePoint 2010 Client Object Model

  1. Pingback: Programmatically upload documents using SharePoint 2010 Client Object Model - My experiments with SharePoint, Azure and .NET using Visual Studio

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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