Create a Sharepoint anonymous link using REST api - sharepoint

I have a PHP application where I retrieve the documents from Sharepoint and display into a list where users can click to see/download the file.
Currently I'm linking to the Sharepoint URL which is protected by login. I need to link to a link that can be accessed by anonymous users.
I can create a share link using the Sharepoint interface and I see in the network tab (google console) that a POST request to https://myclient.sharepoint.com/sites/CustomerPortal/_api/web/Lists(#a1)/GetItemById(#2)/ShareLink?#a1=%27%7B11CD851B%2D1385%2D43DA%2DBB41%2D132AFAAA0927%7D%27&#a2=%274620%27 is made.
When I try to perform the same request via Postman I get the following error:
{
"error": {
"code": "-1, Microsoft.SharePoint.Client.InvalidClientQueryException",
"message": {
"lang": "en-US",
"value": "The expression \"web/Lists(#a1)/GetItemById(#2)/ShareLink\" is not valid."
}
}
}
I tried to create an anonymous link using the endpoint: https://myclient.sharepoint.com/sites/CustomerPortal/_api/SP.Web.CreateAnonymousLink
with the following JSON body:
{
"url":"http://myclient.sharepoint.com/CustomerPortal/Folder/file.pdf",
"isEditLink":"true"
}
and the response I get is:
{
"error": {
"code": "-1, System.InvalidOperationException",
"message": {
"lang": "en-US",
"value": "Operation is not valid due to the current state of the object."
}
}
}
How can I create a anonymous link using REST API? Is it possible? Is there any configuration that my client needs to do on Sharepoint side?
Thank you.

Related

Cannot upload inside a shared Sharepoint drive, got 'Entity only allows writes with a JSON Content-Type header'

I am trying to upload a simple text document to a sharepoint shared document folder. My account do have write access within this folder.
To acheive that, I'm using the Microsoft Graph API v1.0.
According to the doc, I'm making the following request: PUT https://graph.microsoft.com/v1.0/sites/{site_id}/drive/items/Shared%20Documents/{name_of_folder}:/text.txt:/content
I've set Content-Type to be text/plain and my body is just 'text'.
The Microsoft Graph API send me the following error:
{
"error": {
"code": "BadRequest",
"message": "Entity only allows writes with a JSON Content-Type header.",
"innerError": {
"date": "2021-08-23T08:10:44",
"request-id": "51fcdf1f-d7d0-4585-b712-ddd0197e046e",
...
}
}
}
I've managed to upload a document using PUT /me/drive/items/{parent-id}:/{filename}:/content
Is there something wrong?

SharePoint Site and Graph API (Beta) - can't find any pages under site and can't create page (access denied)

I am currently working with the SharePoint Graph API (beta) through Postman.
I have it all working and my Azure Active Directory application has the correct scopes for SharePoint.
What I am wondering is this:
I have this site (subsite, sorry my SharePoint terminology might not be the best):
https://graph.microsoft.com/{{api-version}}/sites/COMPANY_NAME.sharepoint.com,070d3261-xxxx-4c69-b186-a83e7ee0b6bb,09bc2cd0-aada-45fb-xxxx-bcdce9d5711e:/kb:/
which returns the site I am looking for (Note: I truncated the json):
{
"#odata.context": "https://graph.microsoft.com/beta/$metadata#sites/$entity",
"createdDateTime": "2020-05-18T06:02:28Z",
"description": "",
"id": "COMPANY_NAME.sharepoint.com,070d3261-xxxx-4c69-b186-a83e7ee0b6bb,e7808145-aab1-xxxx-bccc-df798f6a36eb",
"lastModifiedDateTime": "2020-09-23T21:37:37Z",
"name": "kb",
"displayName": "Knowledge Base - I removed the rest of the json"
}
But when I add /pages to the end:
https://graph.microsoft.com/{{api-version}}/sites/COMPANY_NAME.sharepoint.com,070d3261-xxxx-4c69-b186-a83e7ee0b6bb,09bc2cd0-aada-45fb-xxxx-bcdce9d5711e:/kb:/pages/
I get this:
{
"#odata.context": "https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.sitePage)",
"value": []
}
Now, I just went and created a new page in that site, even though there were already pages there, so I know they exist.
Another thing to note is that when I try to create a new page via the API:
POST https://graph.microsoft.com/{{api-version}}/sites/{{site-id}}/pages
I get this:
{
"error": {
"code": "accessDenied",
"message": "Access denied",
"innerError": {
"date": "2020-09-27T22:56:06",
"request-id": "1306506d-caea-4748-99a9-7eee20bb6404",
"client-request-id": "1306506d-caea-4748-99a9-7eee20bb6404"
}
}
}
Can anyone help me understand why the collection / array in my response does not contain any pages and also, why I cannot create a new page when my AAD App have the required scopes and these scopes have my admins' consent
cheers
Update. Adding some screen shots:
As you said you are using a daemon Application you need to specify right permissions here.
The screen shot shows that you are actually adding it for Sharepoint App permissions, please add the Sites.ReadWrite.All for the Microsoft Graph Permissions. As you can see above the green pointer there are 3 permissions(Directory.Read.All, User.Read.All and User.Read), add "Sites.ReadWrite.All" there to get the site pages and also it helps you in creating the pages.

Creating Azure App Insights using REST API fails requires ROLE

I am generating API Key for an App Insight. I am using the URL
"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Insights/components/{resourceName}/ApiKeys"
I don't have any clear documentation and I found this from the MS SDK:
https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/applicationinsights/Microsoft.Azure.Management.ApplicationInsights/src/Generated/APIKeysOperations.cs
However, when I try to generate by mentioning a "name", an error comes in response:
{
"code": "The API Key needs to have a Role",
"message": "The API Key needs to have a Role",
"innererror": {
"diagnosticcontext": "e1f66da1-9247-459e-a519-6426fa1449d1",
"time": "2019-09-20T07:48:20.2634617Z"
}
}
My POST body is as following:
{
"name": "asimplekeyname"
}
Please help if someone has used this specific API.
You need to include the following properties in the body.
{
"name":"test3",
"linkedReadProperties":[
"/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/microsoft.insights/components/<appinsight-name>/api",
"/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/microsoft.insights/components/<appinsight-name>/agentconfig"
],
"linkedWriteProperties":[
"/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/microsoft.insights/components/<appinsight-name>/annotations"
]
}
The three properties correspond the ones in the portal -> your appinsight -> API Access -> Create API key.
api - Read telemetry
agentconfig - Authenticate SDK control channel
annotations - Write annotations
You need to select at least one of them, inculde in the request body.
For example, you just select the first one as below.
The body should be:
{
"name":"test3",
"linkedReadProperties":[
"/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/microsoft.insights/components/<appinsight-name>/api"
],
"linkedWriteProperties":[]
}

Microsoft Graph InTune Beta API's - Unable to add app to app policy

I am attempting to use Intune specific beta Graph APIs to assign apps to an iOS managed app protection policy. I am able to create the app policies using the endpoint documented below:
https://graph.microsoft.io/en-us/docs/api-reference/beta/api/intune_mam_iosmanagedappprotection_create
When created via API, the app "deployedCount" is 0 until specific apps are added in the admin console UI.
Now, I am attempting to retrieve apps added to any policy by API using the endpoint documented below.
https://graph.microsoft.io/en-us/docs/api-reference/beta/api/intune_mam_mobileappidentifierdeployment_list
I get a 400 bad request response that appears to say this endpoint either does not exist or is not responding correctly. This seems unlike if I mistyped an endpoint.
Response I receive:
{
"error": {
"code": "No method match route template",
"message": "No OData route exists that match template ~/entityset/key/navigation with http verb GET for request /MAMAdmin/MAMAdminFEService/managedAppPolicies('T_338de6df-386d-4f1b-a51c-a0d189c61722')/mobileAppIdentifierDeployments.",
"innerError": {
"request-id": "79514f29-4dca-48a5-a2de-5d14138577d7",
"date": "2017-02-08T17:46:52"
}
}
}
Response if I mistyped:
{
"error": {
"code": "BadRequest",
"message": "Resource not found for the segment 'asdfasdf'.",
"innerError": {
"request-id": "e86d84ab-f062-4780-af3c-9afae6e7bc82",
"date": "2017-02-08T18:53:29"
}
}
}
Hello this is Alemeshet Alemu from MSFT.
Sorry, the API documentation is a bit out sync. Could you try with $expand?
GET /managedAppPolicies/{managedAppPoliciesId}?$expand=mobileAppIdentifierDeployments
Alemeshet Alemu (MSFT)

Resource does not exist or one of its queried reference-property objects are not present

I am trying to add role to Azure AD user using Graph API.
I am forming the post request as specified in this msdn article. But the operation is not succeeded. The returned error is as below.
Request
URL: POST https:// graph.windows.net/myorganization/directoryRoles/474732f7-ab9e-4eb5-8689-f5a078f5ed5b/$links/members?api-version=1.6
post data:
{
"url": "https:// graph.windows.net/myorganization/directoryObjects/63d144b6-e2f2-4a4a-b623-13debfcb0ff0"
}
Response Data:
{
"odata.error": {
"code": "Request_ResourceNotFound",
"message": {
"lang": "en",
"value": "Resource '474732f7-ab9e-4eb5-8689-f5a078f5ed5b' does not exist or one of its queried reference-property objects are not present."
}
}
}
The error message says that the requested resource is not found, but when I list all the available directory roles using Graph API, it lists that particular directory role, so that particular directory role is already activated and its available. I am not sure about the second part ('queried reference-property') of the error message. Even MSDN has no documentation regarding this error.
I tested the API with 3 tenants, in that it failed for 2 tenants and succeeded for 1 tenant.
The below is the partial data(I have copied the role which I am trying to add user) of the request made for listing all available directory roles.
Request URL: GET https: //graph.windows.net/myorganization/directoryRoles?api-version=1.6 HTTP/1.1
Response: (partial)
{
"odata.type": "Microsoft.DirectoryServices.DirectoryRole",
"objectType": "Role",
"objectId": "474732f7-ab9e-4eb5-8689-f5a078f5ed5b",
"deletionTimestamp": null,
"description": "Service Support Administrator has access to perform common support tasks.",
"displayName": "Service Support Administrator",
"isSystem": true,
"roleDisabled": false,
"roleTemplateId": "f023fd81-a637-4b56-95fd-791ac0226033"
},
If I try to fetch the above role alone using the provided API then I am getting the same error.
Request URL : GET https:// graph.windows.net/test.onmicrosoft.com/directoryRoles/474732f7-ab9e-4eb5-8689-f5a078f5ed5b?api-version=1.6 HTTP/1.1
Response:
{"odata.error":{"code":"Request_ResourceNotFound","message":{"lang":"en","value":"Resource '474732f7-ab9e-4eb5-8689-f5a078f5ed5b' does not exist or one of its queried reference-property objects are not present."}}
Application Info:
The Azure AD Application has "Access your organization's directory" access right and I am accessing the application using the Global Administrator account.
I have tried giving "Directory Read Write" permission to the application, but same error is happening.

Resources