I'm developing an application using WinUI 3, PnP Core SDK. The code is retreiving the document libraries and lists of a sharepoint with some parameters.
I want you cbange the code to retrieve a lot more information from the lists and library. Add treenode under the listNode nodes if needed.
For example, but not limited to that : description of the list, list of content types, list of columns, list of the views, etc.
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using PnP.Core.Services;
using PnP.Core.Auth;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PnP.Core.Model.SharePoint;
using PnP.Core.QueryModel;
public class ListInfo
{
public string Title { get; set; }
public ListTemplateType TemplateType { get; set; }
public string ServerRelativeUrl { get; set; }
public bool EnableVersioning { get; set; }
}
public sealed partial class MainWindow : Window
{
private IHost host;
private const string ClientId = "65d32704-49e5-490f-94c0-000000000000";
private const string TenantId = "56740a61-9b73-4c19-9220-000000000000";
private const string RedirectUri = "http://localhost";
private List<ListInfo> listInfos;
public MainWindow()
{
this.InitializeComponent();
}
private async void OnScanClick(object sender, RoutedEventArgs e)
{
try
{
string siteUrl = SiteUrlTextBox.Text;
if (string.IsNullOrEmpty(siteUrl))
{
StatusTextBlock.Text = "Veuillez entrer l'URL du site SharePoint.";
return;
}
StatusTextBlock.Text = "Scan en cours...";
var authProvider = CreateAuthenticationProvider();
ConfigureHost(siteUrl, authProvider);
using var scope = host.Services.CreateScope();
var pnpContextFactory = scope.ServiceProvider.GetRequiredService<IPnPContextFactory>();
using (var context = await pnpContextFactory.CreateAsync("Default"))
{
var lists = await context.Web.Lists
.QueryProperties(l => l.Title, l => l.TemplateType, l => l.EnableVersioning)
.ToListAsync();
listInfos = new List<ListInfo>();
foreach (var list in lists)
{
var rootFolder = await list.RootFolder.GetAsync(f => f.ServerRelativeUrl);
listInfos.Add(new ListInfo
{
Title = list.Title,
TemplateType = list.TemplateType,
ServerRelativeUrl = rootFolder.ServerRelativeUrl,
EnableVersioning = list.EnableVersioning
});
}
DisplayResults(siteUrl, listInfos);
}
}
catch (Exception ex)
{
StatusTextBlock.Text = $"Erreur : {ex.Message}";
System.Diagnostics.Debug.WriteLine($"Erreur : {ex.Message}\nStack Trace : {ex.StackTrace}");
}
}
private InteractiveAuthenticationProvider CreateAuthenticationProvider()
{
return new InteractiveAuthenticationProvider(ClientId, TenantId, new Uri(RedirectUri));
}
private void ConfigureHost(string siteUrl, InteractiveAuthenticationProvider authProvider)
{
host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddPnPCore(options =>
{
options.Sites.Add("Default", new PnP.Core.Services.Builder.Configuration.PnPCoreSiteOptions
{
SiteUrl = siteUrl,
AuthenticationProvider = authProvider
});
});
})
.Build();
}
private void DisplayResults(string siteUrl, List<ListInfo> listInfos)
{
var rootNode = new TreeViewNode()
{
Content = $"{DateTime.Now:yyyyMMdd_HHmm}-?"
};
rootNode.IsExpanded = true;
var siteNode = new TreeViewNode()
{
Content = siteUrl
};
siteNode.IsExpanded = true;
rootNode.Children.Add(siteNode);
var librariesNode = new TreeViewNode()
{
Content = "Bibliothèques"
};
librariesNode.IsExpanded = true;
siteNode.Children.Add(librariesNode);
var listsNode = new TreeViewNode()
{
Content = "Listes"
};
listsNode.IsExpanded = true;
siteNode.Children.Add(listsNode);
foreach (var listInfo in listInfos)
{
var listNode = new TreeViewNode()
{
Content = listInfo.Title
};
listNode.IsExpanded = true;
if (listInfo.TemplateType == ListTemplateType.DocumentLibrary || listInfo.TemplateType == ListTemplateType.WebPageLibrary)
{
librariesNode.Children.Add(listNode);
}
else
{
listsNode.Children.Add(listNode);
}
}
if (ResultsTreeView.RootNodes.Count == 0)
{
ResultsTreeView.RootNodes.Add(rootNode);
}
else
{
ResultsTreeView.RootNodes[0].Children.Add(rootNode);
}
StatusTextBlock.Text = "Scan terminé. Résultats affichés.";
}
private void ResultsTreeView_SelectionChanged(TreeView sender, TreeViewSelectionChangedEventArgs args)
{
if (sender.SelectedNode != null && sender.SelectedNode.Content is string SelectedList)
{
ListInfo selectedItem = listInfos.FirstOrDefault(item => item.Title == SelectedList);
if (selectedItem != null)
{
DetailsTextBlock.Text = $"Titre: {selectedItem.Title}\n" +
$"Type: {selectedItem.TemplateType}\n" +
$"URL: {selectedItem.ServerRelativeUrl}\n" +
$"Versionnage activé: {selectedItem.EnableVersioning}";
}
}
else
{
DetailsTextBlock.Text = "Sélectionnez une liste ou une bibliothèque pour voir les détails.";
}
}
}
}
To enhance your WinUI 3 application to retrieve and display additional information about SharePoint lists and libraries, you'll need to perform the following steps:
Extend the Data Model: Add properties to the ListInfo class to hold additional information such as description, content types, columns, and views.
Update Data Retrieval: Modify the OnScanClick method to fetch the additional properties using the PnP Core SDK.
Enhance the TreeView Display: Update the DisplayResults method to include child nodes under each list/node to display the additional information.
Below are the detailed steps and the updated code to achieve this functionality:
1. Extend the Data Model
First, update the ListInfo class to include additional properties:
csharp
public class ListInfo
{
public string Title { get; set; }
public ListTemplateType TemplateType { get; set; }
public string ServerRelativeUrl { get; set; }
public bool EnableVersioning { get; set; }
public string Description { get; set; }
public List<string> ContentTypes { get; set; }
public List<string> Columns { get; set; }
public List<string> Views { get; set; }
}
2. Update Data Retrieval
Modify the OnScanClick method to retrieve the additional properties for each list. This involves querying the list's description, content types, columns, and views.
csharp
private async void OnScanClick(object sender, RoutedEventArgs e)
{
try
{
string siteUrl = SiteUrlTextBox.Text;
if (string.IsNullOrEmpty(siteUrl))
{
StatusTextBlock.Text = "Veuillez entrer l'URL du site SharePoint.";
return;
}
StatusTextBlock.Text = "Scan en cours...";
var authProvider = CreateAuthenticationProvider();
ConfigureHost(siteUrl, authProvider);
using var scope = host.Services.CreateScope();
var pnpContextFactory = scope.ServiceProvider.GetRequiredService<IPnPContextFactory>();
using (var context = await pnpContextFactory.CreateAsync("Default"))
{
var lists = await context.Web.Lists
.QueryProperties(
l => l.Title,
l => l.TemplateType,
l => l.EnableVersioning,
l => l.Description
)
.ToListAsync();
listInfos = new List<ListInfo>();
foreach (var list in lists)
{
var rootFolder = await list.RootFolder.GetAsync(f => f.ServerRelativeUrl);
// Retrieve Content Types
var contentTypes = await list.ContentTypes
.Query(ct => ct.Name)
.ToListAsync();
var contentTypeNames = contentTypes.Select(ct => ct.Name).ToList();
// Retrieve Columns
var fields = await list.Fields
.Query(f => f.Title)
.ToListAsync();
var columnNames = fields.Select(f => f.Title).ToList();
// Retrieve Views
var views = await list.Views
.Query(v => v.Title)
.ToListAsync();
var viewNames = views.Select(v => v.Title).ToList();
listInfos.Add(new ListInfo
{
Title = list.Title,
TemplateType = list.TemplateType,
ServerRelativeUrl = rootFolder.ServerRelativeUrl,
EnableVersioning = list.EnableVersioning,
Description = list.Description,
ContentTypes = contentTypeNames,
Columns = columnNames,
Views = viewNames
});
}
DisplayResults(siteUrl, listInfos);
}
}
catch (Exception ex)
{
StatusTextBlock.Text = $"Erreur : {ex.Message}";
System.Diagnostics.Debug.WriteLine($"Erreur : {ex.Message}\nStack Trace : {ex.StackTrace}");
}
}
3. Enhance the TreeView Display
Update the DisplayResults method to add child nodes under each list node for description, content types, columns, and views.
csharp
private void DisplayResults(string siteUrl, List<ListInfo> listInfos)
{
var rootNode = new TreeViewNode()
{
Content = $"{DateTime.Now:yyyyMMdd_HHmm}-?"
};
rootNode.IsExpanded = true;
var siteNode = new TreeViewNode()
{
Content = siteUrl
};
siteNode.IsExpanded = true;
rootNode.Children.Add(siteNode);
var librariesNode = new TreeViewNode()
{
Content = "Bibliothèques"
};
librariesNode.IsExpanded = true;
siteNode.Children.Add(librariesNode);
var listsNode = new TreeViewNode()
{
Content = "Listes"
};
listsNode.IsExpanded = true;
siteNode.Children.Add(listsNode);
foreach (var listInfo in listInfos)
{
var listNode = new TreeViewNode()
{
Content = listInfo.Title
};
listNode.IsExpanded = false; // Initially collapsed
// Add child nodes for additional information
var descriptionNode = new TreeViewNode()
{
Content = $"Description: {listInfo.Description}"
};
listNode.Children.Add(descriptionNode);
// Content Types
var contentTypesNode = new TreeViewNode()
{
Content = "Content Types",
IsExpanded = false
};
foreach (var ct in listInfo.ContentTypes)
{
var ctNode = new TreeViewNode()
{
Content = ct
};
contentTypesNode.Children.Add(ctNode);
}
listNode.Children.Add(contentTypesNode);
// Columns
var columnsNode = new TreeViewNode()
{
Content = "Columns",
IsExpanded = false
};
foreach (var column in listInfo.Columns)
{
var columnNode = new TreeViewNode()
{
Content = column
};
columnsNode.Children.Add(columnNode);
}
listNode.Children.Add(columnsNode);
// Views
var viewsNode = new TreeViewNode()
{
Content = "Views",
IsExpanded = false
};
foreach (var view in listInfo.Views)
{
var viewNode = new TreeViewNode()
{
Content = view
};
viewsNode.Children.Add(viewNode);
}
listNode.Children.Add(viewsNode);
// Add the listNode to the appropriate parent node
if (listInfo.TemplateType == ListTemplateType.DocumentLibrary || listInfo.TemplateType == ListTemplateType.WebPageLibrary)
{
librariesNode.Children.Add(listNode);
}
else
{
listsNode.Children.Add(listNode);
}
}
if (ResultsTreeView.RootNodes.Count == 0)
{
ResultsTreeView.RootNodes.Add(rootNode);
}
else
{
ResultsTreeView.RootNodes[0].Children.Add(rootNode);
}
StatusTextBlock.Text = "Scan terminé. Résultats affichés.";
}
4. Update the Details Display (Optional)
If you want the details pane to show more comprehensive information when a node is selected, you can enhance the ResultsTreeView_SelectionChanged method accordingly. Here's an example that displays detailed information based on the node level:
csharp
private void ResultsTreeView_SelectionChanged(TreeView sender, TreeViewSelectionChangedEventArgs args)
{
if (sender.SelectedNode != null)
{
TreeViewNode selectedNode = sender.SelectedNode;
// Traverse up to find if it's a list node or its child
var currentNode = selectedNode;
ListInfo selectedList = null;
while (currentNode != null)
{
if (currentNode.Content is string content && listInfos.Any(l => l.Title == content))
{
selectedList = listInfos.FirstOrDefault(l => l.Title == content);
break;
}
currentNode = currentNode.Parent;
}
if (selectedList != null)
{
DetailsTextBlock.Text = $"Titre: {selectedList.Title}\n" +
$"Type: {selectedList.TemplateType}\n" +
$"URL: {selectedList.ServerRelativeUrl}\n" +
$"Versionnage activé: {selectedList.EnableVersioning}\n" +
$"Description: {selectedList.Description}\n" +
$"Content Types: {string.Join(", ", selectedList.ContentTypes)}\n" +
$"Columns: {string.Join(", ", selectedList.Columns)}\n" +
$"Views: {string.Join(", ", selectedList.Views)}";
return;
}
// If it's a child node, display its content
if (selectedNode.Content is string nodeContent)
{
DetailsTextBlock.Text = nodeContent;
}
else
{
DetailsTextBlock.Text = "Sélectionnez une liste ou une bibliothèque pour voir les détails.";
}
}
else
{
DetailsTextBlock.Text = "Sélectionnez une liste ou une bibliothèque pour voir les détails.";
}
}
5. Full Updated Code
Here is the complete updated MainWindow.xaml.cs integrating all the changes:
csharp
6. Additional Recommendations
Performance Considerations: Fetching additional details for each list can increase the scan time, especially for sites with many lists. Consider implementing lazy loading for child nodes or providing options to select which details to retrieve.
Error Handling: Ensure that each asynchronous call has proper error handling to manage scenarios where certain properties might not be accessible due to permissions or other issues.
UI Enhancements: You might want to implement icons or better hierarchical visualization for the TreeView to make it more user-friendly.
Localization: If your application will support multiple languages, consider localizing the strings used in the TreeView nodes and other UI elements.
By following the steps above, your application will retrieve and display comprehensive information about SharePoint lists and libraries, providing a more detailed and informative user experience.
Not yet included in a published release.
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using PnP.Core.Services;
using PnP.Core.Auth;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PnP.Core.Model.SharePoint;
using PnP.Core.QueryModel;
namespace TestWinUI3ConnectionSharePoint_20240916
{
public class ListInfo
{
public string Title { get; set; }
public ListTemplateType TemplateType { get; set; }
public string ServerRelativeUrl { get; set; }
public bool EnableVersioning { get; set; }
public string Description { get; set; }
public List<string> ContentTypes { get; set; }
public List<string> Columns { get; set; }
public List<string> Views { get; set; }
}
public sealed partial class MainWindow : Window
{
private IHost host;
private const string ClientId = "65d32704-49e5-490f-94c0-000000000000";
private const string TenantId = "56740a61-9b73-4c19-9220-000000000000";
private const string RedirectUri = "http://localhost";
private List<ListInfo> listInfos;
public MainWindow()
{
this.InitializeComponent();
}
private async void OnScanClick(object sender, RoutedEventArgs e)
{
try
{
string siteUrl = SiteUrlTextBox.Text;
if (string.IsNullOrEmpty(siteUrl))
{
StatusTextBlock.Text = "Veuillez entrer l'URL du site SharePoint.";
return;
}
StatusTextBlock.Text = "Scan en cours...";
var authProvider = CreateAuthenticationProvider();
ConfigureHost(siteUrl, authProvider);
using var scope = host.Services.CreateScope();
var pnpContextFactory = scope.ServiceProvider.GetRequiredService<IPnPContextFactory>();
using (var context = await pnpContextFactory.CreateAsync("Default"))
{
var lists = await context.Web.Lists
.QueryProperties(
l => l.Title,
l => l.TemplateType,
l => l.EnableVersioning,
l => l.Description
)
.ToListAsync();
listInfos = new List<ListInfo>();
foreach (var list in lists)
{
var rootFolder = await list.RootFolder.GetAsync(f => f.ServerRelativeUrl);
// Retrieve Content Types
var contentTypes = await list.ContentTypes
.Query(ct => ct.Name)
.ToListAsync();
var contentTypeNames = contentTypes.Select(ct => ct.Name).ToList();
// Retrieve Columns
var fields = await list.Fields
.Query(f => f.Title)
.ToListAsync();
var columnNames = fields.Select(f => f.Title).ToList();
// Retrieve Views
var views = await list.Views
.Query(v => v.Title)
.ToListAsync();
var viewNames = views.Select(v => v.Title).ToList();
listInfos.Add(new ListInfo
{
Title = list.Title,
TemplateType = list.TemplateType,
ServerRelativeUrl = rootFolder.ServerRelativeUrl,
EnableVersioning = list.EnableVersioning,
Description = list.Description,
ContentTypes = contentTypeNames,
Columns = columnNames,
Views = viewNames
});
}
DisplayResults(siteUrl, listInfos);
}
}
catch (Exception ex)
{
StatusTextBlock.Text = $"Erreur : {ex.Message}";
System.Diagnostics.Debug.WriteLine($"Erreur : {ex.Message}\nStack Trace : {ex.StackTrace}");
}
}
private InteractiveAuthenticationProvider CreateAuthenticationProvider()
{
return new InteractiveAuthenticationProvider(ClientId, TenantId, new Uri(RedirectUri));
}
private void ConfigureHost(string siteUrl, InteractiveAuthenticationProvider authProvider)
{
host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddPnPCore(options =>
{
options.Sites.Add("Default", new PnP.Core.Services.Builder.Configuration.PnPCoreSiteOptions
{
SiteUrl = siteUrl,
AuthenticationProvider = authProvider
});
});
})
.Build();
}
private void DisplayResults(string siteUrl, List<ListInfo> listInfos)
{
var rootNode = new TreeViewNode()
{
Content = $"{DateTime.Now:yyyyMMdd_HHmm}-?"
};
rootNode.IsExpanded = true;
var siteNode = new TreeViewNode()
{
Content = siteUrl
};
siteNode.IsExpanded = true;
rootNode.Children.Add(siteNode);
var librariesNode = new TreeViewNode()
{
Content = "Bibliothèques"
};
librariesNode.IsExpanded = true;
siteNode.Children.Add(librariesNode);
var listsNode = new TreeViewNode()
{
Content = "Listes"
};
listsNode.IsExpanded = true;
siteNode.Children.Add(listsNode);
foreach (var listInfo in listInfos)
{
var listNode = new TreeViewNode()
{
Content = listInfo.Title
};
listNode.IsExpanded = false; // Initially collapsed
// Add child nodes for additional information
var descriptionNode = new TreeViewNode()
{
Content = $"Description: {listInfo.Description}"
};
listNode.Children.Add(descriptionNode);
// Content Types
var contentTypesNode = new TreeViewNode()
{
Content = "Content Types",
IsExpanded = false
};
foreach (var ct in listInfo.ContentTypes)
{
var ctNode = new TreeViewNode()
{
Content = ct
};
contentTypesNode.Children.Add(ctNode);
}
listNode.Children.Add(contentTypesNode);
// Columns
var columnsNode = new TreeViewNode()
{
Content = "Columns",
IsExpanded = false
};
foreach (var column in listInfo.Columns)
{
var columnNode = new TreeViewNode()
{
Content = column
};
columnsNode.Children.Add(columnNode);
}
listNode.Children.Add(columnsNode);
// Views
var viewsNode = new TreeViewNode()
{
Content = "Views",
IsExpanded = false
};
foreach (var view in listInfo.Views)
{
var viewNode = new TreeViewNode()
{
Content = view
};
viewsNode.Children.Add(viewNode);
}
listNode.Children.Add(viewsNode);
// Add the listNode to the appropriate parent node
if (listInfo.TemplateType == ListTemplateType.DocumentLibrary || listInfo.TemplateType == ListTemplateType.WebPageLibrary)
{
librariesNode.Children.Add(listNode);
}
else
{
listsNode.Children.Add(listNode);
}
}
if (ResultsTreeView.RootNodes.Count == 0)
{
ResultsTreeView.RootNodes.Add(rootNode);
}
else
{
ResultsTreeView.RootNodes[0].Children.Add(rootNode);
}
StatusTextBlock.Text = "Scan terminé. Résultats affichés.";
}
private void ResultsTreeView_SelectionChanged(TreeView sender, TreeViewSelectionChangedEventArgs args)
{
if (sender.SelectedNode != null)
{
TreeViewNode selectedNode = sender.SelectedNode;
// Traverse up to find if it's a list node or its child
var currentNode = selectedNode;
ListInfo selectedList = null;
while (currentNode != null)
{
if (currentNode.Content is string content && listInfos.Any(l => l.Title == content))
{
selectedList = listInfos.FirstOrDefault(l => l.Title == content);
break;
}
currentNode = currentNode.Parent;
}
if (selectedList != null)
{
DetailsTextBlock.Text = $"Titre: {selectedList.Title}\n" +
$"Type: {selectedList.TemplateType}\n" +
$"URL: {selectedList.ServerRelativeUrl}\n" +
$"Versionnage activé: {selectedList.EnableVersioning}\n" +
$"Description: {selectedList.Description}\n" +
$"Content Types: {string.Join(", ", selectedList.ContentTypes)}\n" +
$"Columns: {string.Join(", ", selectedList.Columns)}\n" +
$"Views: {string.Join(", ", selectedList.Views)}";
return;
}
// If it's a child node, display its content
if (selectedNode.Content is string nodeContent)
{
DetailsTextBlock.Text = nodeContent;
}
else
{
DetailsTextBlock.Text = "Sélectionnez une liste ou une bibliothèque pour voir les détails.";
}
}
else
{
DetailsTextBlock.Text = "Sélectionnez une liste ou une bibliothèque pour voir les détails.";
}
}
}
}