0

I have the following code to create a root folder and sub-folders inside my remote event receiver:-

foreach (ListItem i2 in collListItem2)
                        {
var templatenumber = isDeal ? "10" : i2["TemplateNumber"];
CamlQuery camlQuery3 = new CamlQuery();
camlQuery3.ViewXml = string.Format("<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name='Templates' /><Value Type='Text'>{0}</Value></Eq></Where></Query></View>", templatenumber);
ListItemCollection collListItem3 = context.Web.GetList(webrelativeurl + "/lists/" + "Levels").GetItems(camlQuery3);
context.Load(collListItem3, items => items.Include(
                                item => item.Id,
                                item => item["Level0"],
                                item => item["Level1"],
                                item => item["Level2"],
                                item => item["Level3"],
                                item => item["Level4"],
                                item => item["Level5"]
                                ));
context.ExecuteQuery();
List templatelsit = context.Web.GetList(webrelativeurl + "/" + i2["TemplateListUrl"]);
//create the Main Folder

ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();

// Below are options.
// (1) File - This will create a file in the list or document library
// (2) Folder - This will create a foder in list(if folder creation is enabled) or documnt library
listItemCreationInformation.UnderlyingObjectType = FileSystemObjectType.Folder;
listItemCreationInformation.LeafName = i["EntityName"].ToString();
ListItem listItem = templatelsit.AddItem(listItemCreationInformation);
listItem["Title"] = i["EntityName"];
listItem.Update();
ListItemCreationInformation listItemCreationInformation2 = new ListItemCreationInformation();

// create sub-folder
                               listItemCreationInformation2.UnderlyingObjectType = FileSystemObjectType.Folder;

// This will et the internal name/path of the file/folder
listItemCreationInformation2.LeafName = i["EntityName"].ToString() + "/" + i3["Level1"].ToString();

    try
       {
         ListItem listItem2 = templatelsit.AddItem(listItemCreationInformation2);
         listItem2.Update();
       }
         catch { }

now the above code will add the main folder, but will not add the sub-folder. so can anyone advice on this please? Thanks

1

try the below code, I have added context.ExecuteQuery(); in the required places.

foreach (ListItem i2 in collListItem2)
{
var templatenumber = isDeal ? "10" : i2["TemplateNumber"];
CamlQuery camlQuery3 = new CamlQuery();
camlQuery3.ViewXml = string.Format("<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name='Templates' /><Value Type='Text'>{0}</Value></Eq></Where></Query></View>", templatenumber);
ListItemCollection collListItem3 = context.Web.GetList(webrelativeurl + "/lists/" + "Levels").GetItems(camlQuery3);
context.Load(collListItem3, items => items.Include(
                                item => item.Id,
                                item => item["Level0"],
                                item => item["Level1"],
                                item => item["Level2"],
                                item => item["Level3"],
                                item => item["Level4"],
                                item => item["Level5"]
                                ));
context.ExecuteQuery();
List templatelsit = context.Web.GetList(webrelativeurl + "/" + i2["TemplateListUrl"]);
//create the Main Folder

ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();

// Below are options.
// (1) File - This will create a file in the list or document library
// (2) Folder - This will create a foder in list(if folder creation is enabled) or documnt library
listItemCreationInformation.UnderlyingObjectType = FileSystemObjectType.Folder;
listItemCreationInformation.LeafName = i["EntityName"].ToString();
ListItem listItem = templatelsit.AddItem(listItemCreationInformation);
listItem["Title"] = i["EntityName"];
listItem.Update();
context.ExecuteQuery(); // now the folder would have been created

ListItemCreationInformation listItemCreationInformation2 = new ListItemCreationInformation();

// create sub-folder
listItemCreationInformation2.UnderlyingObjectType = FileSystemObjectType.Folder;

// This will et the internal name/path of the file/folder
listItemCreationInformation2.LeafName = i["EntityName"].ToString() + "/" + i3["Level1"].ToString();

    try
       {
         ListItem listItem2 = templatelsit.AddItem(listItemCreationInformation2);
         listItem2.Update();
         context.ExecuteQuery(); // now the sub folder would have been created
       }
         catch { }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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