Ensure Folder Exists based on a Specific Property #1204
-
I am trying to create an extension method on This is the code I have so far: public static async Task<IFolder> EnsureFolderByPropertyNameAsync(this IFolder folder, string propertyValue)
{
var propertyKey = "OriginTemplateFolderName";
var matchingFolder = await folder.Folders.FirstOrDefaultAsync(f => (string)f.Properties[propertyKey] == propertyValue);
if (matchingFolder != null)
{
return matchingFolder;
}
// Folder not found so create it
var newFolder = await folder.AddFolderAsync(propertyValue);
await newFolder.EnsurePropertiesAsync(f => f.Properties);
if ((string) newFolder.Properties[propertyName] != propertyValue)
{
newFolder.Properties[propertyName] = propertyValue;
await newFolder.Properties.UpdateAsync();
}
return newFolder;
} This is the error that's produced when evaluating
Would there be a better/more efficient way to achieve this without the error above? I think the error has something to do with some fundamental misunderstanding I have with the way domain model child properties get loaded on the folder. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I also tried a variation like this to just manually iterate through the properties since the Linq querying doesn't seem to support queries on properties but this produces a foreach (var childFolder in folder.Folders)
{
await childFolder.EnsurePropertiesAsync(f => f.Properties);
if (childFolder.Properties.Values.TryGetValue(propertyKey, out var value) && value.Equals(propertyKey))
{
return childFolder;
}
} |
Beta Was this translation helpful? Give feedback.
-
That's not exactly what I was looking for but I may be able to repurpose that code and use it for when I have to iterate through and find specific files in an existing directory with a specific property value set and find/replace values inside of each of those files. So I thank you for that. However my original method above was for making sure a child folder exists inside of the directory of a given var customerFolder = await EnsureFolderByPropertyNameAsync(rootFolder, customerName);
var locationFolder = await EnsureFolderByPropertyNameAsync(customerFolder, locationName);
var projectFolder = await EnsureFolderByPropertyNameAsync(locationFolder, projectName); The issue I am facing with this method currently is that I can't read the |
Beta Was this translation helpful? Give feedback.
-
For whatever reason the child folders on a await childFolder.EnsurePropertiesAsync(f => f.Properties); // Doesn't work - zero properties on the folder
await childFolder.LoadAsync(f => f.Properties); // Doesn't work - zero properties on the folder
childFolder = await childFolder.GetAsync(f => f.Properties); // Doesn't work - zero properties on the folder I have to manually call back out to the server to get it to load the properties. This worked: public static async Task<IFolder> EnsureFolderByTemplateFolderPropertyAsync(this IFolder folder, string propertyValue)
{
var propertyKey = "BigCorp_OriginTemplateFolderName";
foreach (var childFolder in folder.Folders)
{
var currentFolder = await folder.PnPContext.Web.GetFolderByServerRelativeUrlAsync(childFolder.ServerRelativeUrl, f => f.Properties);
if (currentFolder.Properties.Values.TryGetValue(propertyKey, out var value) && value.Equals(propertyValue))
{
return currentFolder;
}
}
// Folder not found so create it
var newFolder = await folder.AddFolderAsync(propertyValue);
await newFolder.EnsureFolderHasPropertyAsync(propertyKey, propertyValue);
return newFolder;
} It feels like I shouldn't have to do that |
Beta Was this translation helpful? Give feedback.
For whatever reason the child folders on a
folder.Folders
call does not have the properties loaded. That collection always comes back zero items which is the source of my frustration. I'm sure it has to do with how the framework loads items but I would have expected one of these approaches to actually load them:I have to manually call back out to the server to get it to load the p…