1

I'm trying to programmatically change the "Created By" -field of a list item in a SharePoint list. As a development tool, I'm using the Microsoft Graph API python library - msgraph-sdk for python.

I've read a lot about similar issues online, and the PowerAutomate solutions use the validateUpdateListItem REST endpoint, and other solutions seem to first change the list column read only state to false, then update the Author/Created By value.

I assume I need to use the second method, since if I simply try to change the "AuthorLookupId" of a list item, I get 403:

fields = FieldValueSet(
    additional_data={
        "AuthorLookupId": user_item.id,
    }
)
await graph_client.sites.by_site_id(site_id).lists.by_list_id(
    list.id
).items.by_list_item_id(added_item.id).fields.patch(fields)

In the above snippet, the added_item is the return value of a previous addition (POST) to the same list. This addition succeeds and I can see the row inside the list. user_item is a user from the User Information List, so the id should match and work with AuthorLookupId.

However, if I first patch the list's "Created By" field's readonly value to be false, I no longer get 403:

list_cols = (
    await graph_client.sites.by_site_id(site_id)
    .lists.by_list_id(list.id)
    .columns.get()
)

author_col: Optional[ColumnDefinition] = next(
    (col for col in list_cols.value if col.name == "Author"),
    None,
)

patch_auth_col = ColumnDefinition(read_only=False)

await (
    graph_client.sites.by_site_id(site_id)
    .lists.by_list_id(list.id)
    .columns.by_column_definition_id(author_col.id)
    .patch(patch_auth_col)
)

After this patch, the first code snippet doesn't return 403 anymore, and the patch seems to succeed. However, the "Created By" -value inside the list item does not change. Why is that? What am I doing wrong?

1
  • As a follow-up, I just noticed that the list also have "AppAuthor" and "AppEditor" fields and their respective LookupId fields. I tried to set these to None in my patch request in addition to the set-up of the Author and Editor fields, but I've had no luck for now. Mar 4 at 7:36

1 Answer 1

1

I don't work with python but changing the created by value is usually not a problem. Granted, one would be better of not doing it, but sometimes we need to do what we need to do.

Not sure if you've looked at this article, but what they do is:

a) make the created by field editable

b) update the list item value

c) make the created by field read only again

the emphasis goes on the rest endpoint and the author field casing. since you're not getting an error anymore, it could be as simple as using the correct settings

I can't test this since I don't usually work with python but perhaps it can help

Uri: _api/web/lists/getbytitle('Your%20List%20Name')/items('[ID]') The ID you should be able to plug in from dynamic content from other actions (ie: Get Items, When a New Item is Created, etc)

Body: { '__metadata': { 'type': 'SP.Data.Your_x0020_List_x0020_NameListItem' }, 'AuthorId': [Id] } For the [Id] shown, we plug in some dynamic content from our Parse JSON Action. Note: !pay attention here to cAse. Use the dynamic content Id (uppercase i lowercase d) -- and not id (all lowercase) when you choose.

enter image description here

1
  • Thank you for the comment and pointers @Tiago. It certainly seems like I'm on the right track, since changing either/and the "Author" or "AuthorLookupId" (or "Editor", "EditorLookupId") without first manipulating the read only field fail. However, none of these actually work. I've now tried to change only one of them at a time, every one of them at the same time and I also tried to restore the read only field to "True" after altering it. No luck still.. I'm wondering if I should actually be altering the "created_by" field, but I think this field is just a wrapper for the python lib. Mar 4 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.