Get missing auditlog from Management Activity API in Office365

旧城冷巷雨未停 提交于 2019-12-11 04:18:35

问题


Our application calls out-of-the-box Office 365 Management API to retrieve activities and events on files stored in SharePoint Online. However per our experiment, the application can’t seem to retrieve not enough logs.

Example: We upload 1000 files to document library in Sharepoint Online. We receive 8 subscriptiona. Each subscription, we only get maximum 100 logs. Total call API get logs to retrieve 600 logs. Not enough!

Here my code to get subscription

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>();
        }
    }

Here my code to execute Request

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;
    }

Here my code to get audit log from each subscription

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);
                  }

Here my code to parser 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:


I think that you need to use the pagination header.

If the amount of data is too big, the API will return a header entry named NextPageUrl containing an address to be used to request the next page of results. This link (representing the query) will be available for 24 hours.

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

So, if the response contains this header entry, just use the value of NextPageUrl to request more data.

Repeat the process until this header entry doesn't exists anymore.

You can find more information in the Office 365 Management API reference



来源:https://stackoverflow.com/questions/40305925/get-missing-auditlog-from-management-activity-api-in-office365

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!