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();
Related
Is there a Sharepoint API to get entire content of files/folders for a particular folder at once ?
I don't want to use GetFolderByServerRelativePath recursively.
If don't want to use GetFolderByServerRelativePath, you can use CSOM with CAML Query like this:
string password = "pwd";
string account = "user#Tenant.onmicrosoft.com";
var secret = new SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
using (ClientContext ctx = new ClientContext("https://Tenant.sharepoint.com/sites/sitename"))
{
ctx.Credentials = new SharePointOnlineCredentials(account,secret);
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
List list = web.Lists.GetByTitle("Documents");
ctx.Load(list);
ctx.ExecuteQuery();
Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/shared%20documents/");
ctx.Load(folder);
ctx.ExecuteQuery();
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = #"<View Scope='RecursiveAll'>
<Query>
</Query>
</View>";
camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
ListItemCollection listItems = list.GetItems(camlQuery);
ctx.Load(listItems);
ctx.ExecuteQuery();
foreach (var item in listItems)
{
Console.WriteLine(item.FieldValues["FileRef"].ToString());
}
}
will return all folders/subfolders/files together, hopefully, this is what you need.
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)};
}
I'm using C# and CSOM to build an application that creates an event in a SharePoint calendar that I know exists in my O365 subscription. I know O365 is SharePoint 2013, but my application targets SharePoint 2010, so I'm going to have to deal with both versions.
No exceptions are thrown and everything appears to succeed, but the new event does not display in the calendar, even after a page refresh. If I get a collection of items with the same event title, the program-entered event is returned, and appears to contain all the columns set in code.
The CalendarItemCreate function puts data in all the required columns of the calendar. If I search for other calendar items I have hand-entered through the SharePoint calendar, I find them. The only difference I can see between either hand-entered or program-entered events is the "Description" column has ' for the hand-entered events.
Any ideas?
private void CalendarItemCreate(ICalendarItem item) {
using (var context = new ClientContext(_calendarLocation)) {
context.Credentials = _credentials;
var web = context.Web;
var transferScheduleList = web.Lists.GetByTitle(TransferScheduleToken);
var listItemCreationInformation = new ListItemCreationInformation();
var listItem = transferScheduleList.AddItem(listItemCreationInformation);
listItem[TitleToken] = item.EventTitle;
listItem[EventDateToken] = item.EventStartLocal;
listItem[EndDateToken] = item.EventStartLocal.AddMinutes(30);
listItem[DescriptionToken] = string.Empty; //item.EventDescription;
listItem[TransferTypeToken] = item.EventTransferType;
listItem[TransferStatusToken] = item.EventTransferStatus;
listItem[CategoryToken] = "Data Transfer";
listItem[ConfigurationFileLocationToken] = item.ConfigurationFileLocation;
listItem[EventTypeToken] = 0;
listItem[FallDayEventToken] = false;
listItem[FrecurrenceToken] = false;
listItem.Update();
context.ExecuteQuery();
}
The solution was a combination of formatting the dates as strings that SharePoint could understand and data type mismatches in two of my transfer columns. The code below was successful.
using (var context = new ClientContext(_calendarLocation)) {
context.Credentials = _credentials;
var web = context.Web;
var transferScheduleList = web.Lists.GetByTitle(TransferScheduleToken);
var listItemCreationInformation = new ListItemCreationInformation();
var listItem = transferScheduleList.AddItem(listItemCreationInformation);
listItem[TitleToken] = item.EventTitle;
listItem[EventDateToken] = item.EventStartLocal.ToUniversalTime().ToString(#"yyyy-MM-ddTHH:mm:ssZ");
listItem[EndDateToken] = item.EventStartLocal.AddMinutes(30).ToUniversalTime().ToString(#"yyyy-MM-ddTHH:mm:ssZ");
listItem[DescriptionToken] = item.EventDescription;
listItem[TransferTypeToken] = item.EventTransferType.ToString();
listItem[TransferStatusToken] = item.EventTransferStatus.ToString();
listItem[CategoryToken] = "Data Transfer";
listItem[ConfigurationFileLocationToken] = item.ConfigurationFileLocation;
listItem[EventTypeToken] = 0;
listItem[FallDayEventToken] = false;
listItem[FrecurrenceToken] = false;
listItem.Update();
context.ExecuteQuery();
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 =)
I need to create subfolder in sharepoint with client object model, the parent folder is existing, this is my code, but I got this error,
Invalid file name.
The file name you specified could not be used. It may be the name of an existing file or directory, or you may not have permission to access the file.
ContentTypeCollection listContentTypes = list.ContentTypes;
clientContext.Load(listContentTypes, types => types.Include
(type => type.Id, type => type.Name,
type => type.Parent));
var result = clientContext.LoadQuery(listContentTypes.Where
(c => c.Name == "Folder"));
clientContext.ExecuteQuery();
ContentType folderContentType = result.FirstOrDefault();
ListItemCreationInformation newItem = new ListItemCreationInformation();
newItem.UnderlyingObjectType = FileSystemObjectType.Folder;
newItem.FolderUrl = #"http://mysite/sites/org" + "/" + listName;
if (!folderName1.Equals(string.Empty))
{
newItem.FolderUrl += "/" + folderName1;
}
newItem.LeafName = folderName2;
ListItem item = list.AddItem(newItem);
item["ContentTypeId"] = folderContentType.Id.ToString();
item["Title"] = folderName2;
item.Update();
clientContext.Load(list);
clientContext.ExecuteQuery();
If you need to create folder you can try to use the following code:
using (var clientContext = new ClientContext(#"http://server"))
{
var web = clientContext.Web;
var lst = web.Lists.GetByTitle("CheckDocLib");
var fld1 = lst.RootFolder.Folders.Add("FirstLevel1");
var fld2 = fld1.Folders.Add("SecondLevel1");
fld1.Update();
fld2.Update();
clientContext.ExecuteQuery();
}