How to solve Sharepoint CSOM HTTP Request Error 42

2020-03-31 06:46发布

问题:

The sharepoint often displays error 429 (Too many requests) and we have already taken all the actions described in the article below, but some requests continue to be blocked.

https://docs.microsoft.com/en-us/sharepoint/dev/general-development/how-to-avoid-getting-throttled-or-blocked-in-sharepoint-online

Our scenario involves a customized desktop application (add-in) for Word, Excel, Power Point and Outlook that accesses the Sharepoint through CSOM (with user's network credentials) and we already registered this Add-in through the "/_layouts/15/AppRegNew.aspx" page and decorated all of our requests with "NONISV|{OUR ORGANIZATION NAME}|{OUR ADDIN NAME}/1.0 ", as described in:

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/register-sharepoint-add-ins

回答1:

Follow the instructions in this Microsoft article:

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/register-sharepoint-add-ins

When you have your Add-In registered, do this when you are creating SharePoint Context (using CSOM):

    private void Initialize()
    {
        this.SPCurrentContext = new ClientContext(this.Url);

        if (string.IsNullOrWhiteSpace(this.Domain))
        {
            this.SPCurrentContext.Credentials = new SharePointOnlineCredentials(this.User, ParseToSecureString(this.Password));
        }
        else
        {
            this.SPCurrentContext.Credentials = new NetworkCredential(this.User, ParseToSecureString(this.Password), this.Domain);
        }

        this.RetryCount = Properties.Settings.Default.DefaultRetryCount;
        this.RetryDelay = Properties.Settings.Default.DefaultRetryDelay;
        this.NONISV = Properties.Settings.Default.ClientAppNONISV;

        this.SPCurrentContext.ExecutingWebRequest += delegate (object sender, WebRequestEventArgs e)
        {
            e.WebRequestExecutor.WebRequest.UserAgent = this.NONISV; // This is the TRICK!!!
        };
    }

The NONISV used as User-Agent HTTP Header should be something like:

NONISV|{Your Company Name}|{Your Add-In Name}/1.0

As described here. Good luck!