UPLOADING DOCUMENT WITH METADATA USING SHAREPOINT CLIENT OBJECT MODEL
public
Boolean UploadDocumenMeta(
string
fileName,
string
filePath,
string
SPLibrary,
string
SPDestanationPath,Dictionary<
string
,
string
> metadata)
{
try
{
using
(SharepoinClient.ClientContext clientContext =
new
SharepoinClient.ClientContext(_sharePointURL))
{
SharepoinClient.Web Web = clientContext.Web;
SharepoinClient.List List = Web.Lists.GetByTitle(SPLibrary);
SharepoinClient.FileCreationInformation fileInfo =
new
SharepoinClient.FileCreationInformation();
byte
[] filecontent = System.IO.File.ReadAllBytes(filePath);
fileInfo.Content = filecontent;
fileInfo.Url = SPDestanationPath + fileName;
Microsoft.SharePoint.Client.File uploadFile = List.RootFolder.Files.Add(fileInfo);
clientContext.Load(List);
clientContext.ExecuteQuery();
SharepoinClient.ListItem item = uploadFile.ListItemAllFields;
foreach
(KeyValuePair<
string
,
string
> metadataitem
in
metadata)
{
item[metadataitem.Key.ToString()] = metadataitem.Value.ToString();
}
item.Update();
clientContext.ExecuteQuery();
return
true
;
}
}
catch
(Exception ex)
{
throw
new
System.IO.FileNotFoundException(ex.Message, ex);
}
}
public string AddNewForm(string WebUrl, string NewTitle)
{
string strMsg = "";
if (string.IsNullOrEmpty(WebUrl))
return EmptyProcURL;
try
{
// Starting with ClientContext, the constructor requires a URL to the server running SharePoint.
using (ClientContext client = new ClientContext(WebUrl))
{
//client.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Assume that the web site has a library named "FormLibrary".
var formLib = client.Web.Lists.GetByTitle("FormLibrary");
client.Load(formLib.RootFolder);
client.ExecuteQuery();
// FormTemplate path, The path should be on the local machine/server !
string fileName = @"D:\Projects\FormTemplate.xml";
var fileUrl = "";
//Craete FormTemplate and save in the library.
using (var fs = new FileStream(fileName, FileMode.Open))
{
var fi = new FileInfo("newForm.xml");
fileUrl = String.Format("{0}/{1}", formLib.RootFolder.ServerRelativeUrl, fi.Name);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(client, fileUrl, fs, true);
client.ExecuteQuery();
}
// Get library columns collection.
var libFields = formLib.Fields;
client.Load(libFields);
client.ExecuteQuery();
Microsoft.SharePoint.Client.File newFile = client.Web.GetFileByServerRelativeUrl(fileUrl);
ListItem item = newFile.ListItemAllFields;
// Here the index of Title column is 9, you may use this format to update any column (even promoted fields).
// To find the index of interested column you should inspect libFields at debug mode, look in the libFields.Fields collection to find the index!
item[libFields[9].StaticName] = NewTitle ;
item.Update();
client.ExecuteQuery();
}
}
catch (Exception ex)
{
strMsg = ex.Message;
}
return strMsg;
}
---------------------------------------------------------
private void UploadDataToSharepointTest(List<UploadData> pDataObjList)
{
string lServerUrl = @"http://xxxxxxx:2000/";
string lFolderName = DateTime.Now.ToString(@"yyyyMMddHHmmss");
ClientContext context = new ClientContext(lServerUrl);
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new System.Net.NetworkCredential("user", "password", "domain");
Web web = context.Web;
List docs = web.Lists.GetByTitle("ABC");
Folder lNewFolder = web.Folders.Add(lServerUrl + "ABC/" + lFolderName + "/");
docs.Update();
int fileIndex = 1;
foreach (var item in pDataObjList)
{
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(item.CompleteFilePath);
newFile.Url = fileIndex.ToString() + "-" + item.fileName;
fileIndex++;
Microsoft.SharePoint.Client.File uploadFile = lNewFolder.Files.Add(newFile);
context.Load(uploadFile);
context.ExecuteQuery();
Dictionary<string, string> metadata = new Dictionary<string, string>();
metadata.Add("Comments", item.comments);
metadata.Add("Plan_x0020_Size", item.planSize);
metadata.Add("Density", item.density);
metadata.Add("First_x0020_Name", txtFirstName.Text.Trim());
metadata.Add("Last_x0020_Name", txtLastName.Text.Trim());
metadata.Add("Company", txtCompany.Text.Trim());
metadata.Add("Contact", txtContact.Text.Trim());
metadata.Add("Additional_x0020_Comments", txtAdditionalComments.Text.Trim());
Microsoft.SharePoint.Client.ListItem items = uploadFile.ListItemAllFields;
context.Load(items);
context.ExecuteQuery();
foreach (KeyValuePair<string, string> metadataitem in metadata)
{
items[metadataitem.Key.ToString()] = metadataitem.Value.ToString();
}
items.Update();
context.ExecuteQuery();
}
}
No comments:
Post a Comment