locked
Sharing records with the CRM 2011 SDK RRS feed

Answers

  • You should add the following references to your project for custom workflow activity development:

    • Microsoft.Xrm.Sdk
    • Microsoft.Xrm.Sdk.Workflow
    • Microsoft.Crm.Sdk.Proxy

    Make sure that you get the above assemblies from CRM 2011 SDK. 

    This is what I have for the using statements. 

    using System;
    using System.Activities;
    using Microsoft.Crm.Sdk.Messages;
    
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;
    
    Hope this helps. 


    Daniel Cai | http://danielcai.blogspot.com | Tata Solutions Inc.
    • Marked as answer by RobertHess05 Thursday, October 13, 2011 8:44 PM
    Thursday, October 13, 2011 8:14 PM

All replies

  • The error that you have encountered looks a compile-time error to me, it shouldn't be a run-time error. 

    The code itself is fine, I have just tried to run the sample code, I have not experienced any problem. 

    Maybe there are some more details that we don't know here. 


    Daniel Cai | http://danielcai.blogspot.com | Tata Solutions Inc.
    Wednesday, October 12, 2011 10:06 PM
  • thanks Daniel.  It's a custom workflow & needs to be compiled.  Is it possible to compile with the compile-time error?
    Thursday, October 13, 2011 1:55 AM
  • It is not possible to compile with any unresolved compile-time errors. 

    It will help if you can post some of your code here, so one of us can have a look for you. 


    Daniel Cai | http://danielcai.blogspot.com | Tata Solutions Inc.
    Thursday, October 13, 2011 3:49 AM
  • The following custom workflow code should compile for you. 

     

    protected override void Execute(CodeActivityContext executionContext)
    {
        //Create the tracing service
        ITracingService tracingService = executionContext.GetExtension<ITracingService>();
    
        //Create the context
        IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
        IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
        Guid _accountId = /* Specify the account record id*/;
        Guid _userId = /* Specify the system user record id that you want to share the account record */;
        GrantAccessRequest grantRequest = new GrantAccessRequest()
        {
            Target = new EntityReference("account", _accountId),
            PrincipalAccess = new PrincipalAccess()
            {
                Principal = new EntityReference("systemuser", _userId),
                AccessMask = AccessRights.WriteAccess | AccessRights.ReadAccess | AccessRights.ShareAccess
            }
        };
        // Execute the request.
        GrantAccessResponse grantResponse = (GrantAccessResponse)service.Execute(grantRequest);
    }
    

     


    Daniel Cai | http://danielcai.blogspot.com | Tata Solutions Inc.
    Thursday, October 13, 2011 4:07 AM
  • Is GrantAccessRequest  part of the Microsoft.Crm.SdkTypeProxy library?  I get a compile time error on the Target = new EntityReference lines:

    Cannot implicitly convert type 'Microsoft.Xrm.Sdk.EntityReference' to 'Microsoft.Crm.Sdk.SecurityPrincipal'

    Here's my code

    using System;
    using System.IO;
    using System.Activities;
    using System.Collections;
    using System.Reflection;
    using System.Collections.ObjectModel;
    using System.Collections.Generic;
    
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Crm.Sdk;
    
    namespace _2011CustomWorkflowActivity
    {
    
    
        public class CustomWorkflowTemplate : CodeActivity
        {
            string myLog = string.Empty, myNoteSubject = string.Empty;
            IWorkflowContext context;
            IOrganizationServiceFactory serviceFactory;
            IOrganizationService service;
    
            protected override void Execute(CodeActivityContext executionContext)
            {
                //Create the tracing service
                ITracingService tracingService = executionContext.GetExtension<ITracingService>();
    
                //Create the context
                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                Guid _accountId = Guid.Empty;
                Guid _userId = Guid.Empty;
                Microsoft.Crm.SdkTypeProxy.GrantAccessRequest grantRequest = new Microsoft.Crm.SdkTypeProxy.GrantAccessRequest()
                {
    
                    Target = new EntityReference("account", _accountId),
                    PrincipalAccess = new PrincipalAccess()
                    {
                        Principal = new EntityReference("systemuser", _userId),
                        AccessMask = AccessRights.WriteAccess | AccessRights.ReadAccess | AccessRights.ShareAccess
                    }
                };
                // Execute the request.
                Microsoft.Crm.SdkTypeProxy.GrantAccessResponse grantResponse = (Microsoft.Crm.SdkTypeProxy.GrantAccessResponse)service.Execute(grantRequest);
            }
    
        }
    
    }
    

     


    My goal is to simple upgrade my 4.0 code, which works very well in CRM 4.  Here it is:

                          SecurityPrincipal principal = new SecurityPrincipal();
                            principal.Type = SecurityPrincipalType.User;
                            principal.PrincipalId = mia_originalownerid;
                            PrincipalAccess principalAccess = new PrincipalAccess();
                            principalAccess.Principal = principal;
                            principalAccess.AccessMask = AccessRights.ReadAccess;
                            Microsoft.Crm.SdkTypeProxy.TargetOwnedDynamic target = new Microsoft.Crm.SdkTypeProxy.TargetOwnedDynamic();
                            target.EntityName = "mia_repexpense2";
                            target.EntityId = myEntity.Id;
                            Microsoft.Crm.SdkTypeProxy.GrantAccessRequest grant = new Microsoft.Crm.SdkTypeProxy.GrantAccessRequest();
                            grant.PrincipalAccess = principalAccess;
                            grant.Target = target;
                            Microsoft.Crm.SdkTypeProxy.GrantAccessResponse granted = (Microsoft.Crm.SdkTypeProxy.GrantAccessResponse)service.Execute(grant);
    
    


     

     

    Thursday, October 13, 2011 7:06 PM
  • It is probably related to the references that you have added to the project. Do you somehow have CRM 4.0 SDK assembly (Microsoft.Crm.Sdk.dll) referenced in your project? If that's the case, you should remove the reference.  


    Daniel Cai | http://danielcai.blogspot.com | Tata Solutions Inc.
    Thursday, October 13, 2011 7:16 PM
  • I removed it, but now I can's use GrantAccessRequest, PrincipalAccess, AccessRights or GrantAccessResponse.
    What namespace do they exist in 2011?
    Thursday, October 13, 2011 8:08 PM
  • I'm using

    using System;
    using System.IO;
    using System.Activities;
    using System.Collections;
    using System.Reflection;
    using System.Collections.ObjectModel;
    using System.Collections.Generic;
    
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;
    

     

    Thursday, October 13, 2011 8:11 PM
  • You should add the following references to your project for custom workflow activity development:

    • Microsoft.Xrm.Sdk
    • Microsoft.Xrm.Sdk.Workflow
    • Microsoft.Crm.Sdk.Proxy

    Make sure that you get the above assemblies from CRM 2011 SDK. 

    This is what I have for the using statements. 

    using System;
    using System.Activities;
    using Microsoft.Crm.Sdk.Messages;
    
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;
    
    Hope this helps. 


    Daniel Cai | http://danielcai.blogspot.com | Tata Solutions Inc.
    • Marked as answer by RobertHess05 Thursday, October 13, 2011 8:44 PM
    Thursday, October 13, 2011 8:14 PM
  • I readded my references from the latest SDK, and can now compile.  Lesson learned.  Thank you for your persistant help, Daniel.
    Thursday, October 13, 2011 8:45 PM
  • You are welcome. I imagine that you are working on CRM4->2011 project migration, this is quite common. 
    Daniel Cai | http://danielcai.blogspot.com | Tata Solutions Inc.
    Thursday, October 13, 2011 8:48 PM