In Sharepoint 2010 I used the SPSecurity.RunWithElevatedPrivileges and query the AuditLog to know which users viewed which documents inside a full-trust solution.

Is there a way to get this data inside a provider-hosted (high-trust) app for Sharepoint 2013 using server side code?


You cannot use the server side SPAuditQuery api in provider hosted web.

You should be able to achieve this in 3 ways:

1) There is SP.ChangeQuery api available on client side to fetch changes done on lists/Document Library level from sharepoint. You can use ChangeQuery class in CSOM. Refer Use of ChangeQuery in CSOM

2) Generate Http Post requests from your client side code to generate audit excel files in sharepoint server and then download them and read the audit data. **For this approach you may need to schedule some background process which will generate post requests and collect audit information and store in db and then u can use this information in your provider hosted web Below is the code to generate post request for content modification audit report. You need to supply viewstate, requestdigest, and eventvalidation parameters to this code by extracting the same from response of a get request to the same url.*

HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create( hostUrl +"_layouts/15/CustomizeReport.aspx?ReportId="+reportID+"&Category=Auditing");
endpointRequest.Method = "POST";

endpointRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0";

//For Office 365
//Cookie authCookie = o365CookieHelper.getAuthenticationCookie();
//CookieContainer cookies = new CookieContainer();
//endpointRequest.CookieContainer = cookies;

//For On premise
endpointRequest.Credentials = new NetworkCredential(userName, password, domain);

//For content modification report
string body = "MSOWebPartPage_PostbackSource=&MSOTlPn_SelectedWpId=&MSOTlPn_View=0&MSOTlPn_ShowSettings=False&MSOGallery_SelectedLibrary=&MSOGallery_FilterString=&MSOTlPn_Button=none&__EVENTTARGET=&__EVENTARGUMENT=&MSOSPWebPartManager_DisplayModeName=Browse&MSOSPWebPartManager_ExitingDesignMode=false&MSOWebPartPage_Shared=&MSOLayout_LayoutChanges=&MSOLayout_InDesignMode=&MSOSPWebPartManager_OldDisplayModeName=Browse&MSOSPWebPartManager_StartWebPartEditingName=false&MSOSPWebPartManager_EndWebPartEditing=false&_maintainWorkspaceScrollPosition=0&__REQUESTDIGEST=" + requestDigest + "&__VIEWSTATE=" + viewState + "&__SCROLLPOSITIONX=0&__SCROLLPOSITIONY=0&__EVENTVALIDATION=" + eventValidation + "&ctl00%24PlaceHolderMain%24ctl00%24ctl02%24TxtReportStorageLocation=" + spListName + "&ctl00%24PlaceHolderMain%24ctl01%24RptControls%24btnOK=OK";

byte[] byteArray = Encoding.UTF8.GetBytes(body);

endpointRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  "Bearer " + "");
endpointRequest.ContentLength = byteArray.Length;
endpointRequest.ContentType = "application/x-www-form-urlencoded";
endpointRequest.KeepAlive = true;

Stream objStream = endpointRequest.GetRequestStream();

objStream.Write(byteArray, 0, byteArray.Length);
HttpWebResponse endpointResponse =

StreamReader streamReader = new StreamReader(endpointResponse.GetResponseStream(), true);
    string target = streamReader.ReadToEnd();

3) Host a wcf service on sharepoint server and use SPAuditQuery inside service to get audit data. Provider hosted web will consume this service to get audit data.

Based on design constraints you can take decision which way to go..

Hope this helps

  • According to this msdn.microsoft.com/en-us/library/jj245254.aspx SP.ChangeQuery API is JS. I'm looking for something that can be used from server side (I'm using a high-trust STS app so server side code is available, just not full trust code). 2) and 3) doesn't seem to be a viable solution since I need to provide this for different companies, with different environments and I need something that can be all packed-up in a provider-hosted SP app. 3) is what we did on SP2010 full-trust solution. Isn't a way to query the SP.ChangeQuery via CSOM?
    – empz
    Nov 14 '13 at 16:34
  • Can you elaborate or link me to a resource to do number 2? If I can generate a HTTP Post request from the client side that will track (and save to an on-premise sql db) when a user views or modifies a document that would be PERFECT.
    – empz
    Nov 14 '13 at 20:05
  • Did the code posted above helped you emzero, if yes please mark as answer . Thanks Dec 10 '13 at 9:39
  • @emzero Microsoft.SharePoint.Client has ChangeQuery class. ref. msdn.microsoft.com/library/office/…
    – bkk
    Sep 9 '15 at 9:53

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.