Upload File With MetaData Fields - sharepoint

using (ClientContext clientContext = new ClientContext("URLSHAREPOINT"))
{
SecureString passWord = new SecureString();
foreach (char c in "PASSWORD".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("USERNAME.onmicrosoft.com", passWord);
Web web = clientContext.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(#"FILELOCATION");
newFile.Url = "file uploaded via client OM.txt";
List docs = web.Lists.GetByTitle("LIBRARYNAME");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
clientContext.ExecuteQuery();
}
I'am Upload files to my Sharepoint 365 Acount with the code above !
But I need to add Some metadata too
Can someone Help Me To add Metadata using my corrent Code ?

clientContext.Load(docList);
clientContext.Load(docList.Fields.GetByTitle("METADATANAME"));
clientContext.Load(docList.Fields.GetByTitle("METADATANAME"));
clientContext.ExecuteQuery();
var Nome = docList.Fields.GetByTitle("METADATANAME").InternalName;
var Posicao = docList.Fields.GetByTitle("METADATANAME").InternalName;
uploadFile.ListItemAllFields[Nome] = "VALUE";
uploadFile.ListItemAllFields[Posicao] = "VALUE";
uploadFile.ListItemAllFields.Update();
clientContext.Load(uploadFile);
clientContext.ExecuteQuery();
This is working ! Hope to Help Some one with this =)

Related

Sharepoint 2016 on-premise details Item Version field informations

I am working on SharePoint 2016 CSOM to get list item version history. unfortunately i am not getting the field values. please find the code below.
var file = item.File;
var versionFiles = file.Versions;
var fa = file.ListItemAllFields;
clientContext.Load(fa);
clientContext.Load(file);
clientContext.Load(versionFiles);
clientContext.ExecuteQuery();
if (null != versionFiles)
{
var fileVersion = file.Versions[5];
SP.File oldFile =web.GetFileByServerRelativeUrl("/sites/site/_vti_history/1234/list1/file1.pdf");
var allField = oldFile.ListItemAllFields;
clientContext.Load(allField);
}
You could get version history metadata from Lists.asmx.
Sample code:
var web = clientContext.Web;
List spList = clientContext.Web.Lists.GetByTitle("MyDoc");
var item = spList.GetItemById(43);
clientContext.Load(spList);
clientContext.Load(item);
clientContext.ExecuteQuery();
#region customList
Lists.Lists listService = new Lists.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = siteUrl + "/_vti_bin/lists.asmx";
XmlNode nodeAttachments = listService.GetVersionCollection(spList.Id.ToString(), item.Id.ToString(), "Title");
foreach (System.Xml.XmlNode xNode in nodeAttachments)
{
Console.WriteLine(xNode.Attributes["Title"].Value);
}

Change Folder internal name in SharePoint Document Library

I'm having a list of folder names which has lengthy names. Is it possible to change only the internal names of the folders inside SharePoint Document library which will reduce the length of the url?
We can add new column to store the old folder name, then using the code below to set change the folder name in document library.
string targetSiteURL = #"https://xx.sharepoint.com/sites/xx";
var login = "xx#xxx.onmicrosoft.com";
var password = "xxx";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
ClientContext ctx = new ClientContext(targetSiteURL);
ctx.Credentials = onlineCredentials;
var list = ctx.Web.Lists.GetByTitle("Documents");
var folderItem = list.GetItemById(12);
folderItem["FileLeafRef"] = "TestFolder";
folderItem.Update();
ctx.ExecuteQuery();

How can I create new site (not subsite ) in sharepoint online using CSOM

I want to create new site at root level (not subsite in existing site). I am using CSOM for creating site. In current scenario I need to provide the Site URL to client context for authentication and perform operations. Here is a pseudo code.
string url = "https://mysharepoint.com/sites/testsite";
SecureString f_SecurePass = new SecureString();
foreach (char ch in pass)
f_SecurePass.AppendChar(ch);
clientcontext = new ClientContext(url);
var credentials = new SharePointOnlineCredentials(userid, f_SecurePass);
clientcontext.Credentials = credentials;
Web web = clientcontext.Web;
clientcontext.Load(web, website => website.Lists);
clientcontext.ExecuteQuery();
WebCreationInformation wci = new WebCreationInformation();
wci.Url = "/TestAPISite2";
wci.Title = "TestAPISite2";
wci.Language = 1033;
var newsite = clientcontext.Site.RootWeb.Webs.Add(wci);
clientcontext.ExecuteQuery();
Please suggest the solution.
Regarding the requirement:
I want to create new site at root level (not subsite in existing
site).
in SharePoint terminology it is called a site collection:
site collection / top level site / parent site - a site collection
is a web site that can contain sub-sites (aka webs)
In SharePoint CSOM API Tenant class is intended for that purpose, in particular Tenant.CreateSite method.
Here is an example:
const string username = "username#contoso.onmicrosoft.com";
const string password = "password";
const string tenantAdminUrl = "https://contoso-admin.sharepoint.com/";
using (var ctx = GetContext(tenantAdminUrl,userName,password))
{
CreateSite(ctx, "https://contoso.sharepoint.com/sites/marketing","Marketing");
}
where
internal static void CreateSite(ClientContext context, String url, String owner, String title =null, String template = null, uint? localeId = null, int? compatibilityLevel = null, long? storageQuota = null, double? resourceQuota = null, int? timeZoneId = null)
{
var tenant = new Tenant(context);
if (url == null)
throw new ArgumentException("Site Url must be specified");
if (string.IsNullOrEmpty(owner))
throw new ArgumentException("Site Owner must be specified");
var siteCreationProperties = new SiteCreationProperties {Url = url, Owner = owner};
if (!string.IsNullOrEmpty(template))
siteCreationProperties.Template = template;
if (!string.IsNullOrEmpty(title))
siteCreationProperties.Title = title;
if (localeId.HasValue)
siteCreationProperties.Lcid = localeId.Value;
if (compatibilityLevel.HasValue)
siteCreationProperties.CompatibilityLevel = compatibilityLevel.Value;
if (storageQuota.HasValue)
siteCreationProperties.StorageMaximumLevel = storageQuota.Value;
if (resourceQuota.HasValue)
siteCreationProperties.UserCodeMaximumLevel = resourceQuota.Value;
if (timeZoneId.HasValue)
siteCreationProperties.TimeZoneId = timeZoneId.Value;
var siteOp = tenant.CreateSite(siteCreationProperties);
context.Load(siteOp);
context.ExecuteQuery();
}
and
public static ClientContext GetContext(string url, string userName, string password)
{
var securePassword = new SecureString();
foreach (var ch in password) securePassword.AppendChar(ch);
return new ClientContext(url) {Credentials = new SharePointOnlineCredentials(userName, securePassword)};
}

"Version conflict." error while trying to update list item via CSOM

I am getting a "Version conflict" error while trying to update a list item via CSOM.
Code:
String webUrl = <Your Weburl>;
String path = row["Path"].ToString();
String listTitle = null;
try
{
using (ClientContext ctx = new ClientContext(webUrl))
{
ctx.Credentials = new SharePointOnlineCredentials(tenantsitelogin, password);
listTitle = "Pages"; // path.Substring(0, path.LastIndexOf("/")).Substring(webUrl.Length + 1);
var list = ctx.Web.Lists.GetByTitle(listTitle);
var listItem = list.GetItemById(184);
ctx.Load(list);
ctx.Load(listItem, i => i.File);
var file = listItem.File;
ctx.Load(file, x => x.Versions);
ctx.ExecuteQuery();
file.UnPublish("Some Comment");
listItem.Update();
ctx.ExecuteQuery();
//ctx.Web.Lists.GetByTitle(listTitle);
}
}
Need to resolve the "Version conflict" error.
Remove following line from your code to resolve “Version conflict.” error
listItem.Update();

Rename the Uploaded Document in document library using the CSOM

I uploaded the document in SharePoint document library using CSOM. Now I need to Rename the uploaded file using CSOM, here is my code
Web web = context.Web;
bool iscontinue = false;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(InputFileName);
newFile.Url = "/" + newFile;
List docs = web.Lists.GetByTitle(ConfigurationManager.AppSettings["DocumentLibraryName"]);
string strFilename = InputFileName;
//rename
List docs = web.Lists.GetByTitle("Shared Documents");
docs = web.Lists.GetByTitle(Convert.ToString(ConfigurationManager.AppSettings["DocumentLibraryName"]));
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
context.Load(uploadFile);
context.ExecuteQuery();
ListItem item = uploadFile.ListItemAllFields;
string renamefile = txtfilename.Text.ToString();
string docTitle = string.Empty;
item["Title"] = renamefile;
item.Update();
RenameFile(context, strFilename, renamefile);

Resources