2016-10-28 46 views
0

我們的應用程序調用開箱即用的Office 365管理API來檢索存儲在SharePoint Online中的文件的活動和事件。但是,根據我們的實驗,應用程序似乎無法檢索到不足的日誌。從Office 365中的管理活動API獲取缺少審覈日誌

示例:我們上傳1000個文件到Sharepoint Online中的文檔庫。我們收到8份訂閱。每次訂閱,我們只能獲得最多100個日誌。總計調用API獲取日誌以檢索600個日誌。不夠!

這裏我的代碼來獲取訂閱

List<SubscriptionsContent> GetSubscriptionsContents(AuthenticationResult authenticationResult, ManagementAPI m, DateTime startDate, DateTime endDate, bool proxyRequired = false) 
    { 
     try 
     { 
      string jsonSubscription = string.Empty; 
      string url = string.Empty; 
      string logType = "Audit.SharePoint"; 

      if (authenticationResult != null) 
      { 
       url = string.Format(UrlFormat, m.TenantId, string.Format("subscriptions/content?contentType={0}&startTime={1}&endTime={2}", logType, startDate.ToUniversalTime().ToString(DateFormat), endDate.ToUniversalTime().ToString(DateFormat))); 
       jsonSubscription = ExecuteRequest(url, HttpMethod.Get, authenticationResult); 
       //Log.Info("jsonSubscription:"); 
       //Log.Info(jsonSubscription); 
      } 
      var listContent = Common.GetListSubscriptionsContent(jsonSubscription); 
      Log.Info("Common.GetListSubscriptionsContent(jsonSubscription); Count: " + (listContent != null ? listContent.Count.ToString() : "IS NULL")); 
      return listContent; 
     } 
     catch (Exception ex) 
     { 
      Log.Error(ex); 
      return new List<SubscriptionsContent>(); 
     } 
    } 

這裏我的代碼執行請求

public string ExecuteRequest(string url, HttpMethod method, AuthenticationResult token) 
    { 
     var responseStr = ""; 
     try 
     { 
      HttpClient client = new HttpClient(); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
      HttpRequestMessage request = new HttpRequestMessage(method, url); 
      request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken); 
      HttpResponseMessage response = client.SendAsync(request).Result; 
      Log.Info("ExecuteRequest(string url, HttpMethod method, AuthenticationResult token): response.StatusCode: " + response.StatusCode + " ; response.ReasonPhrase: " + response.ReasonPhrase + " ; response.RequestMessage: " + response.RequestMessage); 


      if (response.IsSuccessStatusCode) 
      { 
       responseStr = response.Content.ReadAsStringAsync().Result; 
      } 
     } 
     catch (Exception ex) 
     { 
      Log.Error(ex); 
     } 

     return responseStr; 
    } 

這裏我的代碼從每個訂閱

List<AuditLog> listAudit = new List<AuditLog>(); 
       foreach (var item in listSubscription) 
       { 
        var jsonAudit = ExecuteRequest(item.ContentUri.ToString(), HttpMethod.Get, authenticationResult); 

        if (string.IsNullOrEmpty(jsonAudit)) 
         continue; 
        var listAuditLog = Common.GetListAuditLog(jsonAudit); 
        } 

這裏我的代碼獲取的審計日誌解析器JsonString

public static List<AuditLog> GetListAuditLog(string jsonString) 
    { 
     try 
     { 
      return JsonConvert.DeserializeObject<List<AuditLog>>(jsonString); 
     } 
     catch (Exception ex) 
     { 
      Log.Error("public static List<AuditLog> GetListAuditLog(string jsonString)", ex.InnerException); 
      return new List<AuditLog>(); 
     } 
    } 

回答

1

我認爲你需要使用分頁標題。

如果數據量太大,API將返回一個名爲NextPageUrl的標題條目,其中包含用於請求下一頁結果的地址。此鏈接(代表查詢)將在24小時內提供。

Ex。

HTTP/1.1 200 OK 
Content-Type: application/json; charset=utf-8 
NextPageUrl:https://manage.office.com/api/v1/{tenant_id}/activity/feed/subscriptions/content?contentType=Audit.SharePoint&amp;startTime=2015-10-01&amp;endTime=2015-10-02&amp;nextPage=2015101900R022885001761 

因此,如果響應包含此標頭條目,只需使用NextPageUrl的值請求更多數據。

重複此過程,直到此標題條目不再存在。

你可以在Office 365 Management API reference

中找到更多的信息