Create Promoted Links/My Report Library/Asset Library list in SharePoint 2013 using Client Object Model C#

I have already written a blog specifying how to create a List for SharePoint using Client Object Model. You can check that here.Though that code worked for most of the Lists, the List, Promoted Links, Report Library, and Asset Library, could not be created. They were constantly throwing the error, Invalid list template! As far as I was concerned, ListTemplates that were passed to the ListCreationInformation were correct. So why this error?? Here, I must also mention that I was trying to create these Lists on the Web template Team-Site.

The problem was I was only passing the baseTemplate to it. However, for these Lists, I needed to mention the GUID of the Feature, that contained the schema of the new List. Following is the modified code.

Namespace:

using Microsoft.SharePoint.Client;

//use one of the templateName that suits your purpose
string templateName = "Promoted Links";string templateName = "Report Library";string templateName = "Asset Library";

ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 

Web web = ctx.Web;

// Get the list template by name
ListTemplate listTemplate = web.ListTemplates.GetByName(templateName);

ctx.Load(listTemplate);
ctx.ExecuteQuery();

// Create a new object for ListCreationInformation class - used to specify the // properties of the new list
ListCreationInformation creationInfo = new ListCreationInformation();

// Specify the title of your List
creationInfo.Title = "My Custom List";

// Specify the list description, if any
creationInfo.Description = "Description";

// Set a value that specifies the feature identifier of the feature 
// that contains the list schema for the new list.
creationInfo.TemplateFeatureId = listTemplate.FeatureId;

// Set a value that specifies the list server template of the new list
creationInfo.TemplateType = listTemplate.ListTemplateTypeKind;

web.Lists.Add(creationInfo);
ctx.ExecuteQuery();
  

Create a SharePoint View with Custom Url Client Object Model C#

In Client Object Model, you don’t have any option to set the url of the view that you’re creating. The url is automatically set as per the Title of your View. So if your Views title is say, My View, then its url will become My%20View. So how you can set the url as MyView instead of the My%20View?

Well, there’s an easy way-out. While creating the View, set its Title to the value that you want its url to have. So, for the above ex, create the view with the title MyView, so that its url will become MyView. Then afterwards, once the view is created, rename the title as you wish say, My View, your url however will not change and you’ll get your new view with the custom title at the desired location.

ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 

List list = ctx.Web.Lists.GetByTitle("ListTitle");
SP.ViewCreationInformation viewCreationInformation = new SP.ViewCreationInformation();

//setting the title as per the desired url
viewCreationInformation.Title = "MyView";
viewCreationInformation.ViewTypeKind = (SP.ViewType)Enum.Parse(typeof(SP.ViewType), "HTML", true);
viewCreationInformation.RowLimit = 30;
viewCreationInformation.Query = "";
viewCreationInformation.SetAsDefaultView = true;

View newView = list.Views.Add(viewCreationInformation);

this.ctx.Load(list.Views);this.ctx.ExecuteQuery;

//set your custom title once your view has been created at the desired location/url
newView.Title = "My View";
newView.Update();
this.ctx.ExecuteQuery;

Update a UserMulti Column value in Client Object Model C#

Update the value of User/UserMulti column in SharePoint using Client Object Model
ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 

List list = ctx.Web.Lists.GetByTitle("ListTitle");
ListItem currentItem = list.GetItemById(1);

FieldUserValue[] userValueCollection = new FieldUserValue[1];

//Get all the users of this Web
UserCollection userCollection = this.ctx.Web.SiteUsers; 
this.ctx.Load(userCollection, w => w.Include(p => p.Id, p => p.Title));
this.ctx.ExecuteQuery(this.ctx);

User user = userCollection.FirstOrDefault(p => p.Title.ToLower().Trim() == userTitle.ToLower().Trim());

//Making sure that a user of title userTitle is present in this Web
if (user != null)
{
    FieldUserValue fieldUserVal = new FieldUserValue();
    fieldUserVal.LookupId = user.Id;
    userValueCollection.SetValue(fieldUserVal, 0);
}

currentItem["MultiUserValCol"] = userValueCollection;
currentItem.Update(); 
this.ctx.ExecuteQuery(); 

As you can see that updating a multiUser column is along the same line as updating a LookupMulti column. Hence, there are a couple of things that one should take care of while updating this column. Those are mentioned in my previous blog where I have discussed about updating a LookupMulti column. here. https://realmpksharepoint.wordpress.com/2014/01/19/update-a-lookupmulti-column-value-in-sharepoint-using-client-object-model-c/

Update a LookupMulti Column value in SharePoint using Client Object Model C#

Update the value of LookupMulti column in SharePoint using the Client Object Model
ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 

List list = ctx.Web.Lists.GetByTitle("ListTitle");
ListItem currentItem = list.GetItemById(1);

FieldLookupValue[] lookupFieldValCollection = new FieldLookupValue[3];FieldLookupValue lookupFieldA = new FieldLookupValue();
lookupFieldA.LookupId = 2;
lookupFieldValCollection.SetValue(lookupFieldA, 0);
FieldLookupValue lookupFieldB = new FieldLookupValue();
lookupFieldB.LookupId = 4;
lookupFieldValCollection.SetValue(lookupFieldB, 1);
FieldLookupValue lookupFieldC = new FieldLookupValue();
lookupFieldC.LookupId = 6;
lookupFieldValCollection.SetValue(lookupFieldC, 2);

currentItem["MultiLookupValCol"] = lookupFieldValCollection;
currentItem.Update();

ctx.ExecuteQuery();  

Two important thing I would like to share here,

  • Initially I tried to pass an array of an array of integer [containing the ids of the item] to the LookupMulti column which generated the error: Value does not fall between the specified range
  • Another thing was then I created a List of type FieldLookupValue and then I passed the List to the column using its ToArray(). I guess we just had to pass the array of FieldLookupValue. However, this resulted in the error: Unknown Error. Don’t know what that means.

Finally, I can say that create the array of type FieldLookupValue and populate this array using its SetValue method only.

SharePoint Client Object Model Error: The file or folder name contains characters that are not permitted. Please use a different name.

One of the big drawbacks of the SharePoint Client Object Model (CSOM) is that it has very little documentation. This error was thrown when I tried to create a new List using the class ListCreationInformation for a Developer Site. ListCreationInformation class has one property, Url. Now msdn says, it Gets or sets value that specifies the site-relative URL of the location for the new list. http://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.listcreationinformation.url.aspxI thought as with other cases, I am supposed to enter the serverRelativeUrl here. Hence I set a value something like sites/ParentWebUrl/ListRootFolderName. Now when I tried to run the code I got this error:: The file or folder name contains characters that are not permitted. Please use a different name.

Next, just for the sake of testing, I passed only the ListRootFolderName and, the code ran successfully! The List was successfully created with proper values.

I have only faced this issue for a Developer Site. I have tested this condition on Publishing Site, Blog, & Developer Site only.

Still following is a short list of characters that cannot be set as a file/folder names in SharePoint

Site names, subsite names, or site group names

You cannot use the following characters anywhere in a site name, in a subsite name, or in a site or Active Directory group name:

  • Tilde (~)
  • Number sign (#)
  • Percent (%)
  • Ampersand (&)
  • Asterisk (*)
  • Braces ({ })
  • Backslash (\)
  • Colon (:)
  • Angle brackets ()
  • Question mark (?)
  • Slash (/)
  • Plus sign (+)
  • Pipe (|)
  • Quotation mark (“)
  • You cannot start a site name, a subsite name, or a site group name with an underscore (_) character or with the period (.) character.
  • When you create a site name, a subsite name, or a site group name, you cannot use strings that were already used to name managed paths.
  • You cannot use the period character consecutively in the middle of a site name, a subsite name, or a site group name.
  • You cannot use the period character at the end of a site name, a subsite name, or a site group name.

Folder names

You cannot use the following characters anywhere in a folder name or a server name:
  • Tilde
  • Number sign
  • Percent
  • Ampersand
  • Asterisk
  • Braces
  • Backslash
  • Colon
  • Angle brackets
  • Question mark
  • Slash
  • Pipe
  • Quotation mark
  • You cannot use the period character consecutively in the middle of a folder name.
  • You cannot use the period character at the end of a folder name.
  • You cannot start a folder name with a period character.

File names

You cannot use the following characters anywhere in a file name:
  • Tilde
  • Number sign
  • Percent
  • Ampersand
  • Asterisk
  • Braces
  • Backslash
  • Colon
  • Angle brackets
  • Question mark
  • Slash
  • Pipe
  • Quotation mark
  • You cannot use the period character consecutively in the middle of a file name.
  • You cannot use the period character at the end of a file name.
  • You cannot start a file name by using the period character.
  • In addition, file names and folder names may not end with any of the following strings:
    1. .files
    2. _files
    3. -Dateien
    4. _bestanden
    5. _file
    6. _archivos
    7. -filer
    8. _tiedostot
    9. _pliki
    10. _soubory
    11. _elemei
    12. _ficheiros
    13. _arquivos
    14. _dosyalar
    15. _datoteke
    16. _fitxers
    17. _failid
    18. _fails
    19. _bylos
    20. _fajlovi
    21. _fitxategiak

Full details can be found here.