Tuesday, May 19, 2020

Servicepointmanager does not support proxies with the https scheme


I started getting this in VS2015 on one of my machines. I had an additional package feed on myget which constantly asked me for credentials and failed even when entering correct credentials. 
What helped me was clearing the nuget cache and config by deleting these two nuget folders:
  1.          %APPDATA%\NuGet
  2.          %LOCALAPPDATA%\NuGet


After that I restarted Visual Studio and added my custom package source again.

Refresh CRM Page

Hard refresh CRM Page using the below scripts

window.location.reload(true);
//location.reload();

Wednesday, August 14, 2019

Set Default view in inline Lookup D365


function loadCustomDefaultView(){
debugger;
var subgrid= Xrm.Page.getControl("subgridname");
    if (subgrid != null || subgrid != 'undefined' || subgrid !='')) {
Xrm.Page.getControl("subgridname").setDefaultView("GUID");
   }
}

Update the content of Activated Quote in CRM


We got a situation as, the quote should activated for triggering a workflow and from workflow on based on some conditions we need to update the same quote. This is not possible as the quote becomes Read-Only immediately after you activate. 
 
If we want to update it, the option is revising the quote which closes the current quote and creates a new quote where we don't want to create new quotes.
 
After some search, we found a way for doing it and the way is very simple. Just change the state of quote to Draft, update the data and then change the quote state to Active which will not create any new quote.
 
We can do this using a system workflow and also from SDK.
 
System Workflow
 
SDK
// Change the Quote to Draft State
SetStateRequest draftQuote = new SetStateRequest();
draftQuote.EntityMoniker = new EntityReference("quote"newGuid(""));
draftQuote.State = new OptionSetValue(0); // Draft
draftQuote.Status = new OptionSetValue(1); // InProgress
objService.Execute(draftQuote);

// Update the Quote
Entity entQuote = new Entity("quote");
entQuote.Attributes["name"] = "testing1";
entQuote.Id = new Guid("DA5768B5-D62A-E511-80E0-FC15B4283778");
objService.Update(entQuote);

// Change the Quote to Active State.
SetStateRequest activateQuote = new SetStateRequest();
activateQuote.EntityMoniker = new EntityReference("quote"newGuid(""));
activateQuote.State = new OptionSetValue(1); // Active
activateQuote.Status = new OptionSetValue(2); // InProgress
objService.Execute(activateQuote);

Hope this helps.

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.

Tuesday, August 13, 2019

I came across a situation where we need to find the Custom Workflow activity across Processes, to achieve this we can add the Plugin assembly and look for dependencies that's not at all possible in all the cases. 

To overcome this, I had written below FetchXML and executed this in XRMToolBox-->FetchXML Tester

<fetch mapping="logical" count="5000" version="1.0" >
    <entity name="workflow" >
        <attribute name="name" />
        <attribute name="category" />
        <link-entity name="dependency" from="dependentcomponentobjectid" to="workflowid" alias="dep" >
            <link-entity name="plugintype" from="plugintypeid" to="requiredcomponentobjectid" alias="pt" >
                <attribute name="assemblyname" />
                <attribute name="description" />
                <attribute name="friendlyname" />
                <attribute name="name" />
                <attribute name="typename" />
                <attribute name="workflowactivitygroupname" />
                <attribute name="isworkflowactivity" />
            </link-entity>
        </link-entity>
    </entity>
</fetch>

Friday, July 27, 2018

Retrieve All Organizations in CRM

using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.ServiceModel.Description;

namespace GetAllOrganizations
{
    static class Program
    {
        private static string DiscoverServiceURL = ConfigurationManager.AppSettings["DiscoverServiceURL"];
        private static string UserName = ConfigurationManager.AppSettings["UserName"];
        private static string Password = ConfigurationManager.AppSettings["Password"];
        private static ClientCredentials credentials = new ClientCredentials();

        public static OrganizationServiceProxy GetOrganizationService(Organization selectedOrganization)
        {
            credentials.UserName.UserName = UserName;
            credentials.UserName.Password = Password;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(new Uri(selectedOrganization.OrgServiceURL), null, credentials, null);
            return serviceProxy;
        }

        public static List<Organization> GetOrganizations()
        {
            credentials.UserName.UserName = UserName;
            credentials.UserName.Password = Password;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            using (var discoveryProxy = new DiscoveryServiceProxy(new Uri(DiscoverServiceURL), null, credentials, null))
            {
                discoveryProxy.Authenticate();

                // Get all Organizations using Discovery Service
                RetrieveOrganizationsRequest retrieveOrganizationsRequest = new RetrieveOrganizationsRequest()
                {
                    AccessType = EndpointAccessType.Default,
                    Release = OrganizationRelease.Current
                };

                RetrieveOrganizationsResponse retrieveOrganizationsResponse =
                (RetrieveOrganizationsResponse)discoveryProxy.Execute(retrieveOrganizationsRequest);

                if (retrieveOrganizationsResponse.Details.Count > 0)
                {
                    var orgs = new List<Organization>();
                    foreach (OrganizationDetail orgInfo in retrieveOrganizationsResponse.Details)
                    {
                        string orgServUrl = string.Empty;
                        orgInfo.Endpoints.TryGetValue(EndpointType.OrganizationService, out orgServUrl);
                        orgs.Add(new Organization()
                        {
                            OrgFriendlyName = orgInfo.FriendlyName,
                            OrgUniqueName = orgInfo.UniqueName,
                            OrgServiceURL = orgServUrl
                        });
                    }
                    return orgs;
                }
                else
                    return null;
            }
        }
    }

    public class Organization
    {
        public string OrgFriendlyName { get; set; }
        public string OrgUniqueName { get; set; }
        public string OrgServiceURL { get; set; }
    }
}