Tips & Tricks | Undocumented SDK Requests | PowerApps [SharePoint]

Document management capabilities of SharePoint from within a Microsoft Dataverse model-driven or customer engagement app.

With SharePoint, you can store and manage documents in the context of a record on a SharePoint Server, and implement the SharePoint infrastructure to share, manage, and collaborate efficiently. Because SharePoint stores documents on a SharePoint Server, users who aren't running the app can directly access documents on the SharePoint Server. This is provided that users have the appropriate permissions.

For document management functionality, you enable server-based SharePoint integration on a site collection in SharePoint. 


In order to extend your solution, you may need to do some requirements programmatically Ex: Upload, Delete, Create Location...


You can achieve that in multiple ways, for example, using Cloud Flow, Sometimes you need to achieve that in client-side or real-time plugins. 

Below are some examples of how you can athe chieve that using Undocumented actions

  Upload

In order to upload a file, you can use "UploadDocument" request this will require thThis isbelow parameters:

Content: Uploaded file as Base64String

OverwriteExisting: Is to identify if it should overwrite the files with the same name or not.

ParentEntityReference: The uploaded file will be regarding "Account, Contact...".

Entity: SharepointDocument Entity contains file title information

You the can execute this rthe equest from Server-side and client-side

Server-side: C# Example

OrganizationRequest reqUpload = new OrganizationRequest("UploadDocument");
 
Entity sharepointdocumentEntity = new Entity("sharepointdocument");
sharepointdocumentEntity.Attributes["locationid"] = "";
sharepointdocumentEntity.Attributes["title"] = (string)annotationEntity.Attributes[Annotation.Fields.FileName];

reqUpload["Content"] = Convert.FromBase64String((string)annotationEntity.Attributes[Annotation.Fields.DocumentBody]);
reqUpload["OverwriteExisting"] = true;
reqUpload["ParentEntityReference"] = regardingObject;
reqUpload["Entity"] = sharepointdocumentEntity;
reqUpload["FolderPath"] = "";
OrganizationResponse response = service.Execute(reqUpload);

  Delete

In order to delete file, you can use "DeleteDocumentrequest this will require the below parameters:

Entities: SharepointDocument EntityCollection contains file to be deleted.ParentEntityReference: The deleted files are regarding "Account, Contact...".

You can execute this request from Server-side and client-side

Server-side: C# Example
var query_isrecursivefetch = true;
string regardingPrimaryIdAttribute = "accountid";//EntityPrimaryIdAttribute example "accountid, contactid

var query = new QueryExpression("sharepointdocument");
query.ColumnSet.AddColumns("documentid""fullname""relativelocation""sharepointcreatedon""filetype""absoluteurl""modified""sharepointmodifiedby""title""readurl""editurl""author""sharepointdocumentid""ischeckedout""locationid""iconclassname");
query.AddOrder("relativelocation", OrderType.Ascending);
query.Criteria.AddCondition("isrecursivefetch", ConditionOperator.Equal, query_isrecursivefetch);
 
var bb = query.AddLink(regardingObject.LogicalName, "regardingobjectid", regardingPrimaryIdAttribute);
bb.EntityAlias = "bb";
bb.LinkCriteria.AddCondition(regardingPrimaryIdAttribute, ConditionOperator.Equal, regardingObject.Id);
 
EntityCollection sharePointDocumentsEC = service.RetrieveMultiple(query);
 
OrganizationRequest reqDelete = new OrganizationRequest("DeleteDocument");
reqDelete["Entities"] = sharePointDocumentsEC;
reqDelete["ParentEntityReference"] = regardingObject;
OrganizationResponse responseDelete = service.Execute(reqDelete);


  Create Document Location

In order to upload a file, you need to create a document location first.

The default behavior of the Platform is when you navigate to the SharePoint iFrame in Dataverse/D365 system will create a Document Location if it is not exist

We will use the same logic to create the it programmatically from JS, C# even Cloud Flow. Just trigger below query

You can execute this request from Server-side and client-side

Server-side: C# Example
var query_isrecursivefetch = true;
string regardingPrimaryIdAttribute = "accountid";//EntityPrimaryIdAttribute example "accountid, contactid

var query = new QueryExpression("sharepointdocument");
query.ColumnSet.AddColumns("documentid""fullname""relativelocation""sharepointcreatedon""filetype""absoluteurl""modified""sharepointmodifiedby""title""readurl""editurl""author""sharepointdocumentid""ischeckedout""locationid""iconclassname");
query.AddOrder("relativelocation", OrderType.Ascending);
query.Criteria.AddCondition("isrecursivefetch", ConditionOperator.Equal, query_isrecursivefetch);
 
var bb = query.AddLink(regardingObject.LogicalName, "regardingobjectid", regardingPrimaryIdAttribute);
bb.EntityAlias = "bb";
bb.LinkCriteria.AddCondition(regardingPrimaryIdAttribute, ConditionOperator.Equal, regardingObject.Id);
 
EntityCollection sharePointDocumentsEC = service.RetrieveMultiple(query);
Retrieve Files

In order to retrievethe the uploaded files, Regarding a specific record, you can use below query

fullname: File Name. absoluteurl: SharePoint File URL. filetype: File Type

You can execute this request from Server-side and client-side

Server-side: C# Example
var query_isrecursivefetch = true;
string regardingPrimaryIdAttribute = "accountid";//EntityPrimaryIdAttribute example "accountid, contactid

var query = new QueryExpression("sharepointdocument");
query.ColumnSet.AddColumns("documentid""fullname""relativelocation""sharepointcreatedon""filetype""absoluteurl""modified""sharepointmodifiedby""title""readurl""editurl""author""sharepointdocumentid""ischeckedout""locationid""iconclassname");
query.AddOrder("relativelocation", OrderType.Ascending);
query.Criteria.AddCondition("isrecursivefetch", ConditionOperator.Equal, query_isrecursivefetch);
 
var bb = query.AddLink(regardingObject.LogicalName, "regardingobjectid", regardingPrimaryIdAttribute);
bb.EntityAlias = "bb";
bb.LinkCriteria.AddCondition(regardingPrimaryIdAttribute, ConditionOperator.Equal, regardingObject.Id);
 
EntityCollection sharePointDocumentsEC = service.RetrieveMultiple(query);
Genarate PowerApps Portal URL

If you are using PowerApps Portal and Enables SharePoint integration and need to understand how the system generates download URL for the uploaded file.

Server-side: C# Example

string.Format("/_entity/sharepointdocumentlocation/{0}?file={1}", documentLocation.Id, entity.Attributes["fullname"])

Popular posts from this blog

Tips & Tricks | Solution Layers [Remove Active Customization]

Tips & Tricks | Power Apps Community Plan [Development Environment]