Wednesday, 5 March 2014

View Tenant (ULS) Logs in SharePoint Online using CSOM

Update (14th Sep 2014): Even though the classes exist in the CSOM, the Microsoft Office 365 Engineering team has confirmed that this is something which is not possible right now. It is also something which is not currently on their road map. They have said that if there is high demand for this feature, they will look into implementing it. Please go to the Office 365 UserVoice site and vote this feature up if you are interested: http://officespdev.uservoice.com/forums/224641-general/suggestions/4578386-provide-a-way-to-view-uls-logs-on-sharepoint-onlin

----------------------------------------------------------X-------------------------------------------------

A while ago, I came across the Office365 UserVoice site. It was a good way to give feedback and let the SharePoint/Office365 team know which features could make the life of SharePoint Developers easy.

I decided to post on it a feature request, which I felt was desperately needed and would really help me as a SharePoint Developer: A way to view ULS Logs on SharePoint Online
http://officespdev.uservoice.com/forums/224641-general/suggestions/4578386-provide-a-way-to-view-uls-logs-on-sharepoint-onlin

It seems this was a shared opinion among many people and the request went on to become one of the top voted on the site:
http://officespdev.uservoice.com/forums/224641-general/filters/top

Fast Forward to today and I was really excited about the SharePoint Conference 2014! I saw a lot of code samples and articles being published as a result of the new features introduced by the SharePoint Team. While exploring the Office App Model Samples (http://officeams.codeplex.com/) I came across the following DLL: Microsoft.Online.SharePoint.Client.Tenant.dll

You can refer to my previous post about using this DLL to create Site Collections in SharePoint Online:
http://www.vrdmn.com/2014/03/create-site-collections-with-csom-in.html

I opened up the DLL in ILSpy and immediately noticed the TenantLog class:
http://msdn.microsoft.com/en-us/library/microsoft.online.sharepoint.tenantadministration.tenantlog_members(v=office.15).aspx

After playing around it a bit, I was able to retrieve Log messages from my Tenant. This seems to be a preview feature still in development and was not working on all the tenants on which I tested. In fact, it was working only on a newly created Tenant! That too only for the Developer Site.

Having a look at the MSDN docs for the SPO PowerShell cmdlet:
http://technet.microsoft.com/en-us/library/fp161369(v=office.15).aspx

"This function cannot retrieve all SharePoint Online errors. It retrieves a subset of errors that happen due to external systems.
For Beta 2, the only logs available are for Business Connectivity Services (BCS)."

I will keep this post updated with any new information which comes along.

You will need the following DLLs for this code to work:
Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll
Microsoft.Online.SharePoint.Client.Tenant.dll

The first two assemblies can be found in the ISAPI folder of your SharePoint 2013 Server Box. The Microsoft.Online.SharePoint.Client.Tenant.dll is a part of  the SharePoint Server 2013 Client Components SDK which can downloaded from here: http://www.microsoft.com/en-in/download/details.aspx?id=35585

So without much further ado, here is the code:
And here is the output I got after running the code:


I think the SharePoint team has done a really great job of listening to the community to add new features to SharePoint/Office365 and I am really happy!

13 comments:

Geetanjali Arora said...

Great post and great job !!

mbakirov said...

we should make a Sharepoint App based on that.

Shahzad said...

Hi There,

Thanks for the great post when I'm trying to use your code, I am getting "Invalid URI: The hostname could not be parsed."

Although I have tried different sites; admin, team and developer sites:
https://mydomain-admin.sharepoint.com
https://mydomain.sharepoint.com
https://mydomain.sharepoint.com/sites/Developer

Vardhaman Deshpande said...

Hi Shahzad,

Have you put the correct username and password for your tenant in the code?

You should replace "password" and "admin@yoursite.onmicrosoft.com" with the credentials of your Tenant Administrator.

Komakino said...

Thanks for sharing. I don't get an exception, but it doesn't return a single ULS entry.

Maybe I don't have have exception entries in my tenant. MS probably doesn't log informational data to the ULS logs / or only pass exception log entries.

madefromatear said...

I have converted this into powershell CSOM but get the following exception even though I'm loading credentials:

Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden."

Durga said...

Hi,

When i execute this code , i get following error

"Proxy Authentication Required"

Can you pls help ?

Durga

Vardhaman Deshpande said...

Are you calling the code from a console application?

TroyB said...

I've tried this code today and receive the same error as Komakino where there aren't any log items returned.

Vardhaman can you post the code and dll's you are using? Maybe it only works for a specific version of the dlls?

Vardhaman Deshpande said...

This functionality seems to be still in beta stage and hence only BCS logs are available.

I was using the 15 version DLLs if that helps.

IT Pro said...

Every time I run this I just end up with... Press Any Key to Exit... with zero log entries. Any ideas what would cause that?

Vardhaman Deshpande said...

Hi, please check my September update at the top of the post. Although the classes exist in the CSOM, Microsoft has not created a server side implementation for it yet.

Binaya Acharya said...

I get this error message while trying with SP2013 Online

An unhandled exception of type 'Microsoft.SharePoint.Client.ServerException' occurred in Microsoft.SharePoint.Client.Runtime.dll

Additional information: Unknown Error

in this line tenantContext.ExecuteQuery();