2

I would like to create a menu from the listtemplate Promoted Links, with several tiles. I use the following code to create the list, and create new list items:

ListTemplate listTemplate = web.ListTemplates.GetByName("Promoted Links"); 

        context.Load(listTemplate);

        // Execute the query to the server
        context.ExecuteQuery();

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

        // TITEL
        creationInfo.Title = "KlasseSite menu"; 
        // description
        creationInfo.Description = "Tile menu";
        creationInfo.TemplateFeatureId = listTemplate.FeatureId;
        creationInfo.TemplateType = listTemplate.ListTemplateTypeKind;
        web.Lists.Add(creationInfo);

        context.ExecuteQuery();  

        // new list item
        List announcementsList = context.Web.Lists.GetByTitle("KlasseSite menu"); 
        ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
        ListItem newItem = announcementsList.AddItem(itemCreateInfo);

        newItem["Title"] = "SkoleSite"; newItem["LinkLocation"] = "/sites/Engelsk";
        newItem["Title"] = "Settings"; newItem["LinkLocation"] = "/sites/Engelsk/Shared%20Documents/Forms/AllItems.aspx";
        newItem.Update();

        context.ExecuteQuery();

My problem: This code only creates one tile, because

newItem["Title"] = "Settings"; newItem["LinkLocation"] = "/sites/Engelsk/Shared%20Documents/Forms/AllItems.aspx";

Overwrites:

newItem["Title"] = "SkoleSite"; newItem["LinkLocation"] = "/sites/Engelsk";

How can i add to several rows in the list?

3 Answers 3

3

Change the code that creates the items to the following:

var itemData = new [,] { { "SkoleSite", "/sites/Engelsk" }, { "Settings", "/sites/Engelsk/Shared%20Documents/Forms/AllItems.aspx" } };
for (var i = 0; i < 2; i++) 
{ 
    var itemCreateInfo = new ListItemCreationInformation();
    var newItem = announcementsList.AddItem(itemCreateInfo);

    newItem["Title"] = itemData[i,0]; newItem["LinkLocation"] = itemData[i,1];
    newItem.Update();
    context.ExecuteQuery(); // I can't remember if this needs to be called after each itme, or if you can just call it once after the loop.
}
2

In your example you are just creating a single list item.

Since CSOM supports Request Batching you could take advantage of it when creating multiple list items as demonstrated in the below example:

var list = context.Web.Lists.GetByTitle("Contacts");
for (var i = 0; i < 4; i++)
{
    var itemCreateInfo = new ListItemCreationInformation() { };
    var newItem = list.AddItem(itemCreateInfo);
    newItem["Title"] = "New Contact" + (i+1);
    newItem.Update();    
 }
 context.ExecuteQuery(); 
1

Before proceeding for adding the second item, you need to do an Update for the first item. In a way you need to do an update for each and every item you add. P.S. You can run a for loop sort of thing to skip repetition of code lines.

EDIT - You may have a look at these links - Link1 & Link2

2
  • Thank you very much! Could you please provide an example, i am quite new in coding. Commented Aug 14, 2014 at 13:20
  • The edit might help you.
    – Amit
    Commented Aug 14, 2014 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.