How to Update properties of a OneDrive item using REST API? - sharepoint

I am trying to update the properties of a OneDrive item using SharePoint REST API.
OneDrive item web URL:
https://vx13-my.sharepoint.com/personal/alexw_vx13_onmicrosoft_com/Documents/SrcDir/File-To-Update.txt
Mainly looking to updating following fields: Author, Created_x0020_By, Editor, Modified_x0020_By, Modified, Created
I tried using Graph API:
Request:
Method: PATCH |
URL: https://graph.microsoft.com/v1.0/sites/vx13-my.sharepoint.com,eab581b1-a945-4d23-9c8e-ec67bb74a42d,32fa1468-54b6-40d6-abbd-f775c4c3932b/drives/b!sYG16kWpI02cjuxnu3SkLWgU-jK2VNZAq733dcTDkyvIAAMdpSy_Sryiw8ARQ8Gv/root:/SrcDir/File-To-Update.txt:/listItem/fields |
Header:
Content-Type: application/json |
Accept: application/json;odata=verbose |
Body:
{
"Created": "2019-02-01 10:25 AM",
"Modified": "2020-01-27 11:25 AM",
"Modified_x0020_By": "3",
"Created_x0020_By": "3",
"Author": "Alex Wilber",
"Editor": "Alex Wilber"
}
It is giving failure response as:
{"code": "accessDenied", "message": "Field 'Created' is read-only"}
Through CSOM API using ValidateUpdateListItem() its working & successfully updating the property fields of OneDrive items.
But is there any way to update property fields of OneDrive item through REST API?

By default, these properties are Readonly by and cannot be edited.
You need to set the ReadOnlyField to false to update these fields. I used to answer the same question here: https://learn.microsoft.com/en-us/answers/questions/221106/rest-api-to-create-item-with-custom-created-by-and.html
For Created By column:
Url: /_api/web/lists/getbytitle('Mylist')/fields/getbytitle('Created By')
Body:
{
"__metadata": { "type": "SP.FieldUser"},
"ReadOnlyField": false
}
For Created column:
Url: /_api/web/lists/getbytitle('Mylist')/fields/getbytitle('Created')
Body:
{
"__metadata": { "type": "SP.FieldDateTime"},
"ReadOnlyField": false
}

Using SharePoint REST API,
a work around to update the Properties of OneDrive item using method ValidateUpdateListItem()
Get the list-item-id of the file
GET https://vx13-my.sharepoint.com/personal/alexw_vx13_onmicrosoft_com/_api/web/GetFileByServerRelativePath(decodedurl='/personal/alexw_vx13_onmicrosoft_com/Documents/SrcDir/File-To-Update.txt')/listitemallfields/fieldvaluesastext?$select=Id
Update Author, Editor, Created & Modified fields of list item using ValidateUpdateListItem
POST https://vx13-my.sharepoint.com/personal/alexw_vx13_onmicrosoft_com/_api/web/Lists/GetByTitle('Documents')/items(<list-item-id>)/ValidateUpdateListItem()
Content-Type:application/json
Accept:application/json;odata=verbose
Body:
{
"formValues":
[
{
"FieldName": "Created",
"FieldValue": "2019/02/01 10:25 AM"
},
{
"FieldName": "Modified",
"FieldValue": "2020/01/27 11:25 AM"
},
{
"FieldName": "Author",
"FieldValue": "[{'Key':'i:0#.f|membership|test_user_AK#vx13.onmicrosoft.com'}]"
},
{
"FieldName": "Editor",
"FieldValue": "[{'Key':'i:0#.f|membership|test_user_JV#vx13.onmicrosoft.com'}]"
}
],
"bNewDocumentUpdate": true
}

If you refer to the documentation here under the Properties section, you will notice certain fields are read only and you wont be able to update them using Graph API.
For all other properties(e.g. name), follow this documentation to update.
Being said that, you can always request for a feature by filling a User Voice so that it goes into our backlog.

Related

Graph API: Unable to programmatically upload OneDrive item with specific properties (failing for createdBy and lastModifiedDataTime)

I am uploading an item to OneDrive using Graph API. I am also setting the properties of the item after it has been uploaded successfully. I am able to set "lastModifiedDateTime" but am not able to set "createdBy" and "createdDateTime".
"createdBy" is always set to the Azure AD application I have created for OAuth and in OneDrive UI it always shows "modified By" "SharePoint App".
And the "createdDataTime" is always current time (time of upload). Is there any way I can set these properties correctly?
The json I am using to patch the item properties:
{"createdDateTime":"2020-12-28T12:25:39Z",
"lastModifiedDateTime":"2020-12-28T12:25:39Z",
"createdBy":
{
"user":{
"email":"AlexW#vx2.onmicrosoft.com"}
},
"lastModifiedBy":{
"user":{
"email":"AlexW#vx2.onmicrosoft.com"}
},
"fileSystemInfo":{
"lastModifiedDateTime":"2020-12-28T12:25:39Z",
"createdDateTime":"2020-12-28T12:25:39Z"},
"file":{"mimeType":"image/jpeg"}
}
Please find the properties (queries from graph explorer) after the upload and above patch request:
{
"createdDateTime": "2020-12-28T12:28:09Z",
"lastModifiedDateTime": "2020-12-28T12:25:39Z",
"createdBy":
{
"application": {
"displayName": "ConsoleApp"}
},
"fileSystemInfo": {
"createdDateTime": "2020-12-28T12:28:09Z",
"lastModifiedDateTime": "2020-12-28T12:25:39Z"
},
"file": {
"mimeType": "image/jpeg",
"hashes": {
"quickXorHash": "4EQEGnBnLd04VXEmYqGHHIeZ2po="
}
}
}
As you can see user name has been replaced by the Azure AD app name and created by time is the time the upload was done and not the time specified in the patch request.
Please let me know if anyone has any idea about this.
If you refer the below article : https://learn.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0 under the Properties section.
These are read-only fields meaning you will not be able to manually configure the values for the same.
WorkAround :
Having said that this cannot be achieved through Graph API however, you can make use of the Sharepoint API to update the same.
ValidateUpdateListItem()
For modifiying the created by , last modified by and last modified the sample body would be of below :
{ formValues": [
{
"FieldName": "Editor",
"FieldValue": "[{'Key':'i:0#.w|AlexW#vx2.onmicrosoft.com'}]"
},
{
"FieldName": "Author",
"FieldValue": "[{'Key':'i:0#.w|AlexW#vx2.onmicrosoft.com'}]"
},
{
"FieldName": "Created",
"FieldValue": "02/18/2020 11:25 PM"
}
],
"bNewDocumentUpdate": true
}
Request URL :
https://SPOURL/_api/web/Lists/GetbyTitle('Library Name')/items(1)/ValidateUpdateListItem"

Syntax error when querying Custom fields with Exclamation Mark via Contract-Based REST API

Why some custom fields contain an exclamation mark in their name ex.: Tasks_ParentNoteID!Subject and CustomerClassRecord.LocaleName!translatedName? I'm getting the schema of custom fields of an entity using the contract-based REST API (https://help-2020r1.acumatica.com/(W(13))/Help?ScreenId=ShowWiki&pageid=64daacf1-75c4-4bfa-b57b-36222020e7c9).
The URL I'm using is as below:
GET /entity/DefaultExt/17.200.001/Contact/$adHocSchema
The sample from the JSON response is as below:
"custom": {
"Contact": {
"ContactID!displayName": {
"type": "CustomStringField",
"value": null
},
"NoteText": {
"type": "CustomStringField",
"value": null
},
"NoteID": {
"type": "CustomGuidField",
"value": null
}
},
"ContactCurrent2": { .......
How do I query this fields using the contract-based REST API since using the exact name:
GET /entity/DefaultExt/17.200.001/Contact?$skip=0&$top=1000&$Custom=Contact.ContactID!displayName
I get the error message:
exceptionMessage=Syntax error: character '!' is not valid at position
17 in 'Contact.ContactID!displayName'.

How to get the Site Id of SharePoint using Graph SharePoint Explorer

How to get the Microsoft SharePoint SiteID using Microsoft Graph API Explorer.
Initially i tried with below API i able to get the Site ID
https://graph.microsoft.com/v1.0/sites/tenantName.sharepoint.com:/sites/TestSite:/drives?select=name,id
Sharepoint URL:
https://tenantName.sharepoint.com/sites/TestSite
I output i got is:
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives",
"value": [
{
"id": "b!l17-JY9YT67Qp-2TBvsUupBLMUF2SrJHp5VylCDZThT7HpCdF-7uQ6NTp6t-MbR5",
"name": "Documents"
}
]
}
But, when i try with Communication Site
Whose SharePoint URL is:
https://tenantName.sharepoint.com/SitePages/DevHome.aspx
Graph Explorer API
https://graph.microsoft.com/v1.0/sites/tenantName.sharepoint.com:/SitePages/DevHome:/drives?select=name,id
I am getting below error:
{
"error": {
"code": "itemNotFound",
"message": "The provided path does not exist, or does not represent a site",
"innerError": {
"request-id": "8329dfca-c63b-4af5-80b8-75f26be9e2e8",
"date": "2019-10-31T13:18:33"
}
}
}
A sitePage is a fundamentally different resource than a site.
A site is a container that owns any number of sub-sites, apps, lists, document libraries, etc.
A sitePage is just another resource owned by a site.
The sitePage resource is currently only available in the Microsoft Graph Beta version.
So the query for /SitePages/DevHome.aspx would be:
/beta/sites/root/pages/{pageId}
If you don't yet know the correct id for the page, you can filter the SitePage collection based on the page's name:
/beta/sites/root/pages?$filter=name eq 'DevHome.aspx'
This will return a collection with a single entity (the DevHome.aspx page):
{
"#odata.context": "https://graph.microsoft.com/beta/$metadata#sites('root')/pages",
"value": [
{
"eTag": "",
"id": "{id}",
"lastModifiedDateTime": "2014-07-10T05:47:29Z",
"name": "DevHome.aspx",
"webUrl": "SitePages/DevHome.aspx",
"createdBy": {
"user": {
"displayName": "System Account"
}
},
"lastModifiedBy": {
"user": {
"displayName": "System Account"
}
},
"parentReference": {
"siteId": "{id}"
},
"contentType": {
"id": "0x0101080062C83F3CFED6744A882F729480DE6C17",
"name": "Wiki Page"
},
"webParts": [],
"publishingState": {
"level": "published",
"versionId": "1.0"
}
}
]
}
I should also point out that you're misinterpreting the result of your first query. When you request /v1.0/sites/{tenant}:/{path}:/drives?select=name,id, you are not getting the IDs for each Site, you're getting the IDs for each Drive within that Site. You can find the objects contained within a site in the Relationships section of the Site Resource documentation
Your Graph API call is incorrect.
Try this one :
https://graph.microsoft.com/v1.0/sites/tenantName.sharepoint.com?select=name,id

Get all root sites listed in share point Admin center using graph api beta

I need all root sites listed in share point Admin center shown in image
For that i am using Graph api like follows,
var settings = {
"async": true,
"crossDomain": true,
"url": "https://graph.microsoft.com/beta/sharepoint/sites",
"method": "GET",
"headers": {
"authorization": "Bearer token",
"cache-control": "no-cache",
"postman-token": "3116b007-e574-5ad4-aedd-3b35fbf76b61"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
But it gives output as follows,
{
"#odata.context": "https://graph.microsoft.com/beta/$metadata#sharePoint/sites",
"value": [
{
"createdDateTime": "2017-02-18T13:03:01.263Z",
"description": "",
"id": "2422c3a2-3c51-40f1-8483-5454aead43c4,412bb897-c754-460a-962d-db22893a1649",
"lastModifiedDateTime": "2017-04-24T02:16:43Z",
"name": "",
"webUrl": "https://mps330124.sharepoint.com",
"root": {},
"siteCollection": {
"hostname": "mps330124.sharepoint.com"
},
"siteCollectionId": "2422c3a2-3c51-40f1-8483-5454aead43c4",
"siteId": "412bb897-c754-460a-962d-db22893a1649"
}
]
}
How can i achieve it Using graph api beta
Using the asterisk on the site search endpoint seems to work
https://graph.microsoft.com/v1.0/sites?search=*
returns
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites",
"value": [
{
"createdDateTime": "2017-09-15T01:11:50Z",
"description": "Let's capture our thoughts in this subsite blog.",
"id": "xyz.sharepoint.com,5a58bb09-1fba-41c1-8125-69da264370a0,5e9767b8-95bc-4bd1-aeb0-defabcdefabc",
"lastModifiedDateTime": "0001-01-01T08:00:00Z",
"name": "internalblogs",
"webUrl": "https://xyz.sharepoint.com/internalblogs",
"displayName": "Internal blog"
},
{
...
}]
}
There isn't a way to retrieve a full list of site collections via Microsoft Graph. That functionality is "admin only" in the SharePoint API and has not been made available via Microsoft Graph.
We're working on some changes to the SharePoint Sites API for Microsoft Graph that will be rolled out over the next few weeks to make it possible to search for a list of sites / site collections, but we still don't have plans to make available the ability to enumerate all sites / site collections via Microsoft Graph.
Watch the Change Log around the Build 2017 conference (May 10-12) to find our all the details.
There now seems to be a way to get a list of the top level site collections via the following request:
GET https://graph.microsoft.com/beta/sites?select=siteCollection,webUrl&filter=siteCollection/root%20ne%20null
The following page documents this (update September 19th): https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/site_list
My own test though didn't bear any fruit, because the API returns a Cannot enumerate sites error. An alternative approach would be to use the search query parameter (docs updated September 20th):
GET https://graph.microsoft.com/beta/sites?search=contoso&select=siteCollection,WebUrl&filter=siteCollection/root ne null
But this only provides the siteCollection for the root :/ with a list of all sites and their webUrl of the other siteCollection's (like the default ones /sites & /teams) but not the actual url/hostname of the siteCollection they belong to. Though that could be extracted from the webUrl path component.
What you are getting is not an error.
If you follow the siteCollectionId with the URL https://graph.microsoft.com/beta/sharepoint/sites/2422c3a2-3c51-40f1-8483-5454aead43c4/sites you get a list of the sites in that site collection.

The property IsDefaultFolder is always false when retrieving taskfolder list with outlook rest api

We are using the outlook-rest api to retrieve a list of taskfolders
GET: https://outlook.office.com/api/v2.0/me/TaskFolders
I tried with a outlook.com and Office 365 account but the property IsDefaultFolder is always false for the default folder.
Can anyone reproduce ? Is this a bug ?
Thanks
See response below
{
"#odata.context": "https://outlook.office.com/api/v2.0/$metadata#Me/TaskFolders",
"value": [{
"#odata.id": "https://outlook.office.com/api/v2.0/Users('xxx')/TaskFolders('ccc')",
"Id": "iii",
"ChangeKey": "ccc",
"Name": "Taken",
"IsDefaultFolder": false,
"ParentGroupKey": "0006f0b7-0000-0000-c000-000000000046"
}],
"#odata.deltaLink": "https://outlook.office.com/api/v2.0/me/TaskFolders/?%24deltatoken=ddd"
}

Resources