msgraph-sdk-dotnet icon indicating copy to clipboard operation
msgraph-sdk-dotnet copied to clipboard

Page.PostAsync always returns Invalid request

Open Thxsoft64 opened this issue 1 year ago • 1 comments

Describe the bug

I am trying to add a page to our site and get an Invalid Request response. I am using many other graph API commands just fine such as reading and uploading files and reading the currently available pages.

The permission in question set is: Sites.Manage.All.

I've tried many code permutations from MS doco, Copilot and the internet and all get the same error.

Authentication is via Microsoft Identity and the Graph API is obtained via AddMicrosoftGraphAppOnly. I also tried using ClientSecretCredential which displayed the same error.

Expected behavior

Either an informative error, or the page to be added to the site.

How to reproduce

The code example is the following which is based on many internet examples (and copilot):

var page = new SitePage()
{
    Title = "a page", 
    PageLayout = PageLayoutType.Article,
    WebParts =
    [
        new TextWebPart
        {
            InnerHtml = "<p>This is a sample content.</p>"
        }
    ],
    PublishingState = new PublicationFacet()
    {
        Level = "published",
        VersionId = "1.0"
    }
};
var newPage = await api.Sites[siteId].Pages.PostAsync(page);

SDK Version

5.77.0

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output

Microsoft.Graph.Models.ODataErrors.ODataError: Invalid request
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.ThrowIfFailedResponseAsync(HttpResponseMessage response, Dictionary`2 errorMapping, Activity activityForAttributes, CancellationToken cancellationToken)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory`1 factory, Dictionary`2 errorMapping, CancellationToken cancellationToken)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory`1 factory, Dictionary`2 errorMapping, CancellationToken cancellationToken)
   at Microsoft.Graph.Sites.Item.Pages.PagesRequestBuilder.PostAsync(BaseSitePage body, Action`1 requestConfiguration, CancellationToken cancellationToken)
   at Program.<Main>$(String[] args) in /Users/steven/Programming/src_w/GeosecureSupport/GeosecureSu//pport/Program.cs:line 115
   at Program.<Main>(String[] args)

Configuration

  • OSX Sequioa - latest
  • M4
  • DotNet 9.0 - latest

Other information

No response

Thxsoft64 avatar May 08 '25 13:05 Thxsoft64

The primary issue is casued becasue OdataType = "#microsoft.graph.sitePage", was missing from the SitePage object. The ibrary normally adds such things automatically.

However, while my test pages do show up in the pages collection returned by the C# API, they are not visible on the SharePoint site. If I use the C# API to delete the page, it does show up in the recycle bin. The SharePoint admin can't work out why we can't see them either.

I tried the example here (https://learn.microsoft.com/en-us/graph/api/sitepage-create?view=graph-rest-1.0&tabs=csharp) and it does not show up in SharePoint either.

Thxsoft64 avatar May 09 '25 02:05 Thxsoft64