Wednesday, August 14, 2019

Get AbsoluteURL from Document Locations in Dynamics 365

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;

namespace DocumentLocationURL

{
    public class DocumentLocationURL : CodeActivity
    {
        #region "Parameter Definition"
        [Input("SharePointDocumentID")]
        [Default("")]
        public InArgument<string> SharePointDocumentID { get; set; }

        [Output("AbsoluteURL")]

        public OutArgument<string> AbsoluteURL { get; set; }

        protected override void Execute(CodeActivityContext executionContext)

        {
            #region "Load CRM Service from context"
            // Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

            if (context == null)

                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            #endregion

            #region "Read Parameters"

            string _FieldName = this.SharePointDocumentID.Get(executionContext);
            EntityReference _Target = null;
            if (this.SharePointDocumentID.Get(executionContext) == null )
                throw new InvalidPluginExecutionException("Enter Target Input.");
           
            #endregion

            #region "AbsoluteURL Execution"

            RetrieveAbsoluteAndSiteCollectionUrlResponse retriveResponse = new RetrieveAbsoluteAndSiteCollectionUrlResponse();

            RetrieveAbsoluteAndSiteCollectionUrlRequest retrieveRequest = new RetrieveAbsoluteAndSiteCollectionUrlRequest

            {
                Target = new EntityReference("sharepointdocumentlocation", context.PrimaryEntityId)//this.SharePointDocumentID.Get(executionContext).Id)
            };
            retriveResponse = (RetrieveAbsoluteAndSiteCollectionUrlResponse)service.Execute(retrieveRequest);
            //throw new InvalidPluginExecutionException(retriveResponse.AbsoluteUrl.ToString());
            this.AbsoluteURL.Set(executionContext, retriveResponse.AbsoluteUrl.ToString());
            #endregion
            

        }

        #endregion

    }

}

Create  Workflow on Document Location entity Create/Update whichever meets to your requirement.

Click on "Add Step" and look for our assembly name i.e DocumentLocationURL, click on this DocumentLocationURL will appear, click on this to add step.

Add update step, and set AbsoluteURL field as DocumentLocationURL  (Output Parameter given in code).

Workflow is completed; save and activate it.

No comments:

Post a Comment