Search code examples
jsonsharepointhttp-headershttprequestsharepoint-online

Getting a "Bad Request" for a GetFileByServerRelativePath/CopyTo for creating a SharePoint Page from a Template


I created a flow that would create a new page from items inputted into a SharePoint list.

The first step is supposed to copy a template file and post it as a new page using the List Item's title name.

My inputs are as follows:

{
    "host": {
        "connectionReferenceName": "shared_sharepointonline",
        "operationId": "HttpRequest"
    },
    "parameters": {
        "dataset": "https://myTenant.sharepoint.com/sites/KSDTestSite",
        "parameters/method": "POST",
        "parameters/uri": "_api/web/GetFileByServerRelativePath(‘/sites/KSDTestSite/SitePages/Templates/KSD-Template.aspx’)/copyTo(‘/sites/KSDTestSite/SitePages/Microsoft_Security_Update_and_EOL_Notification.aspx')",
        "parameters/headers": {
            "Accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose"
        }
    }
}

and the Outputs:

{
    "statusCode": 400,
    "headers": {
        "Pragma": "no-cache",
        "Cache-Control": "no-store, no-cache",
        "Set-Cookie": "ARRAffinity=fa5ce4b13622b0d3617b4398e823c470b20f49c8905d33671c1e72b454b4c01b;Path=/;HttpOnly;Secure;Domain=sharepointonline-ncus.azconn-ncus-001.p.azurewebsites.net,ARRAffinitySameSite=fa5ce4b13622b0d3617b4398e823c470b20f49c8905d33671c1e72b454b4c01b;Path=/;HttpOnly;SameSite=None;Secure;Domain=sharepointonline-ncus.azconn-ncus-001.p.azurewebsites.net",
        "x-ms-request-id": "86e9b9a0-403e-3000-ac9a-13008bc3168f",
        "Strict-Transport-Security": "max-age=31536000; includeSubDomains",
        "X-Content-Type-Options": "nosniff",
        "X-Frame-Options": "DENY",
        "Timing-Allow-Origin": "*",
        "x-ms-apihub-cached-response": "true",
        "x-ms-apihub-obo": "false",
        "Date": "Mon, 05 Jun 2023 16:37:17 GMT",
        "Content-Length": "750",
        "Content-Type": "application/json",
        "Expires": "-1"
    },
    "body": {
        "status": 400,
        "message": "The expression \"web/GetFileByServerRelativePath(‘/sites/KSDTestSite/SitePages/Templates/KSD-Template.aspx’)/copyTo(‘/sites/KSDTestSite/SitePages/Microsoft_Security_Update_and_EOL_Notification.aspx')\" is not valid.\r\nclientRequestId: 5b37d689-d8b9-439f-af4f-2005d6b07e0d\r\nserviceRequestId: 86e9b9a0-403e-3000-ac9a-13008bc3168f",
        "source": "https://myTenant.sharepoint.com/sites/KSDTestSite/_api/web/GetFileByServerRelativePath(%E2%80%98/sites/KSDTestSite/SitePages/Templates/KSD-Template.aspx%E2%80%99)/copyTo(%E2%80%98/sites/KSDTestSite/SitePages/Microsoft_Security_Update_and_EOL_Notification.aspx')",
        "errors": [
            "-1",
            "Microsoft.SharePoint.Client.InvalidClientQueryException"
        ]
    }
}

I keep running into this "Microsoft.SharePoint.Client.InvalidClientQueryException" error, which I've seen it could be a permissions issue? But its a dev environment and there are really no permissions to speak of. Anyways, if anyone has any idea, it would be greatly appreciated.

HTTP Request to SharePoint

Full Error Page Full Error Page


Solution

  • As you find by yourself, there was an issue with quotes in the URI:

    was used instead of '

    Correct syntax is:

    {
        "host": {
            "connectionReferenceName": "shared_sharepointonline",
            "operationId": "HttpRequest"
        },
        "parameters": {
            "dataset": "https://your-tenant.sharepoint.com/sites/KSDTestSite",
            "parameters/method": "POST",
            "parameters/uri": "_api/web/GetFileByServerRelativePath('/sites/KSDTestSite/SitePages/Templates/KSD-Template.aspx')/copyTo('/sites/KSDTestSite/SitePages/Microsoft_Security_Update_and_EOL_Notification.aspx')",
            "parameters/headers": {
                "Accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            }
        }
    }
    

    For security purpose, I remove your tenant root URL. Feel free to edit your question and blur your screenshot as well.