How to upload the document using SharePoint client Object Model and set the Properties

The below code example shows how to upload document from file system to SharePoint using Client Object Model. Also show how to set the properties for Text, Person or group, Checkbox, Managed Metadata columns. Below code refer the excel file to get the file path and column values.

using System;
using Microsoft.SharePoint.Client;
using System.IO;
using System.Data.OleDb;
using System.Data;


class DisplayWebTitle
{
static void Main()
{


string siteUrl = "http://siteurl";
string libraryName = "LibraryName";
string subFolderPath = "";
string metaDataSheetLocation = @"c:\FilePath\FileName.xlsx";

UploadFileInLibrary(siteUrl, libraryName, subFolderPath, metaDataSheetLocation);
}

public static void UploadFileInLibrary(string siteUrl, string libraryName, string subfolderPath, string MetaDataSheetLocation)
{
using (ClientContext clientContext = new ClientContext(siteUrl))
{
DataTable excelData = GetExcelData(MetaDataSheetLocation);


foreach (DataRow dr in excelData.Rows)
{
string fileName = dr["FilePath"].ToString();
string uploadLocation = Path.GetFileName(fileName);
string fileNameStr = Path.GetFileName(fileName);
if (!string.IsNullOrEmpty(subfolderPath))
{
uploadLocation = string.Format("{0}/{1}", subfolderPath, uploadLocation);
}
uploadLocation = string.Format("{0}/{1}", libraryName, uploadLocation);
var list = clientContext.Web.Lists.GetByTitle(libraryName);
var fileCreationInformation = new FileCreationInformation();
fileCreationInformation.Content = System.IO.File.ReadAllBytes(fileName);
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = siteUrl+ uploadLocation;
Microsoft.SharePoint.Client.File uploadFile = list.RootFolder.Files.Add(fileCreationInformation);
Microsoft.SharePoint.Client.ListItem item = uploadFile.ListItemAllFields;
clientContext.ExecuteQuery();
/*--start taxonomy--- */
List taxonomyList = clientContext.Web.Lists.GetByTitle("TaxonomyHiddenList");

CamlQuery camlQueryForTerm = new CamlQuery();
camlQueryForTerm.ViewXml = @"




"+ dr["Col1"].ToString()+"
";

ListItemCollection termItems = taxonomyList.GetItems(camlQueryForTerm);

clientContext.Load(termItems);
clientContext.ExecuteQuery();
ListItem termItem = termItems[0];
string termValue = termItem["ID"] + ";#" + termItem["Term"] + "|" + termItem["IdForTerm"];

/*--End taxanomy --- */


item["TextField"] = dr["TextField"].ToString();

item["PersonGroupField"] = clientContext.Web.EnsureUser(dr["PersonGroupField "].ToString());

item["checkbox"] = dr["checkbox"].ToString();
item.Update()
item["ManagedMetaDataField"] = termValue;
item["ManagedMetaDataFieldTaxHTField0"] = termValue;
item.Update();
clientContext.ExecuteQuery();
Console.WriteLine(fileNameStr + " Uploaded File Successfully");
}



}
}

public static DataTable GetExcelData(string ExcelFilePath)
{
string OledbConnectionString = string.Empty;
OleDbConnection objConn = null;
OledbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFilePath + ";Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(OledbConnectionString);
if (objConn.State == ConnectionState.Closed)
{
objConn.Open();
}
OleDbCommand objCmdSelect = new OleDbCommand("Select * from [Sheet1$]", objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
DataSet objDataset = new DataSet();
objAdapter.Fill(objDataset, "ExcelDataTable");
objConn.Close();
return objDataset.Tables[0];
}

}

Comments

Popular posts from this blog

Microsoft SharePoint Online Code Analysis Framework (MSOCAF)

How to Setup Retention Workflow using Content hub