SharePoint Online: Manage your organization assets library using C# and CSOM
What is organization assets library?
Organization assets library is a feature introduced in My 2019 which lets you register one or more document libraries via PowerShell as a special source for images, such as photos and logos, across all sites. The feature is not available for Office 365 Germany, Office 365 operated by 21Vianet (China), or Office 365 US Government plans.
If your organization needs to store and manage images centrally for all your users to use, you can specify up to 30 document libraries as an organization assets library.
How does it work?
The organization assets library makes your assets readily available and easily accessible for users when they create SharePoint sites and pages.
When a user adds a web part to any modern page in SharePoint and that web part opens the file picker, the user can select "Your organization" in the left pane to browse the libraries you've specified.
Create
In May 2019 a bunch of methods have become available that allow to add, display and remove asset libraries. Method .AddToOrgAssetsLibAndCdn() sets a library to be a central location for organization assets across the tenant. Assets stored within this library become available to sites across the entire tenant. The name publicly displayed for the library will be the name of the library on the SharePoint site.
- First create a ClientContext and test your connection with .ExecuteQuery()
ClientContext ctx = new ClientContext(url);
ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, password);
Tenant spoTenant= new Microsoft.Online.SharePoint.TenantAdministration.Tenant(ctx);
ctx.ExecuteQuery();
- We are changing settings on tenant level, so we need a Microsoft.Online.SharePoint.TenantAdministration.Tenant object.
ctx.Load(spoTenant);
ctx.ExecuteQuery();
spoTenant.AddToOrgAssetsLibAndCdn(SPOTenantCdnType.Public, "https://etr56.sharepoint.com/sites/orgsite/orgassetlib", "https://etr56.sharepoint.com/sites/orgsite/orgassetlib/celebrate.png");
try
{
ctx.ExecuteQuery();
}
catch (Exception e)
{
Console.Write(e.Message);
}
If the library has already been registered as asset library, you will receive an error message: This library is already an organization assets library.
The parameters used are:
- OrgAssetType Type of content in this library. Currently supported value is ImageDocumentLibrary, which is set by default.
- ***LibraryUrl ***Absolute URL of the library to be designated as a central location for organization assets, e.g. "https://etr56.sharepoint.com/sites/orgsite/orgassetlib"
- ThumbnailUrl URL of the background image used when the library is publicly displayed, e.g. "https://etr56.sharepoint.com/sites/orgsite/orgassetlib/libraryLogo.jpg". ThumbnailURL must be on the same site as the library. If no thumbnail URL is indicated, the card will have a gray background.
Display
Method .GetOrgAssets() retrieves organization asset libraries.
- First create a ClientContext and test your connection with .ExecuteQuery()
ClientContext ctx = new ClientContext(url);
ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, password);
Tenant spoTenant= new Microsoft.Online.SharePoint.TenantAdministration.Tenant(ctx);
ctx.ExecuteQuery();
- We are getting settings on tenant level, so we need a Microsoft.Online.SharePoint.TenantAdministration.Tenant object.
ctx.Load(spoTenant);
ctx.ExecuteQuery();
- Use GetOrgAssets().
ClientResult<OrgAssets> orgassets = spoTenant.GetOrgAssets();
ctx.ExecuteQuery();
List<OrgAssetsLibrary> orgassetlibs = orgassets.Value.OrgAssetsLibraries.ToList();
- Display the results. Here, just for the sample, the results are displayed into a console:
foreach (var lib in orgassetlibs)
{
Console.WriteLine();
Console.WriteLine("--------------------------------------------");
Console.WriteLine("Display name: "+lib.DisplayName);
Console.WriteLine("Library url: "+lib.LibraryUrl.DecodedUrl);
Console.WriteLine("File type: "+lib.FileType);
Console.WriteLine("List id: " + lib.ListId);
Console.WriteLine("OrgAssetType: " + lib.OrgAssetType);
Console.WriteLine("ThumbnailUrl: " + lib.ThumbnailUrl.DecodedUrl);
Console.WriteLine("UniqueID: " + lib.UniqueId);
}
Delete
Method **.RemoveFromOrgAssetsAndCdn() **retrieves organization asset libraries.
- First create a ClientContext and test your connection with .ExecuteQuery()
ClientContext ctx = new ClientContext(url);
ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, password);
Tenant spoTenant= new Microsoft.Online.SharePoint.TenantAdministration.Tenant(ctx);
ctx.ExecuteQuery();
- We are getting settings on tenant level, so we need a Microsoft.Online.SharePoint.TenantAdministration.Tenant object.
ctx.Load(spoTenant);
ctx.ExecuteQuery();
- bool remove whether the library should be removed from CDN too
- ***SPOTenantCdnType cdnType *** public or private
- ***string libUrl *** server relative url, e.g. "sites/orgsite/orgassetlib"
spoTenant.RemoveFromOrgAssetsAndCdn(false, SPOTenantCdnType.Public, "https://etr56.sharepoint.com/sites/orgsite/orgassetlib");
try
{
ctx.ExecuteQuery();
Console.WriteLine("Removed");
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
- If the library was not added to CDN, and you set remove to true, you will receive an error: **CDN origin is not found.
**
References
- https://developer.microsoft.com/en-us/sharepoint/blogs/new-sharepoint-csom-version-released-for-sharepoint-online-june-2019/
- https://developer.microsoft.com/en-us/office/blogs/new-sharepoint-csom-version-released-for-sharepoint-online-april-2019/
See Also
- https://developer.microsoft.com/en-us/sharepoint/blogs
- SharePoint Online: Manage your organization assets library using Powershell