Export (0) Print
Expand All

Assign a Predefined Role to a User

CRM 1.0

This sample assigns a predefined security role to a user.

Class Reference

Example

[C#]

public void AssignPredefinedRole()
{
   // strServer should be set with the name of the platform Web server
   String strServer = "MyServerName";

   // strVirtualDirectory should be set with the name of the Microsoft CRM
   // virtual directory on the platform Web server
   String strVirtualDirectory   = "mscrmservices";

   // Create the URL to the SRF files for platform objects
   String strDir = String.Concat("http://", strServer, "/",
                                 strVirtualDirectory, "/");

   // BizUser proxy object
   Microsoft.Crm.Platform.Proxy.BizUser bizUser 
               = new Microsoft.Crm.Platform.Proxy.BizUser ();
   bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
   bizUser.Url = String.Concat(strDir, "BizUser.srf");

   // SecRole proxy object
   Microsoft.Crm.Platform.Proxy.SecRole secRole 
               = new Microsoft.Crm.Platform.Proxy.SecRole ();
   secRole.Credentials = System.Net.CredentialCache.DefaultCredentials;
   secRole.Url = String.Concat(strDir, "SecRole.srf");

   // Declare the caller
   Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = null;

   // Array for role IDs
   String [] strRoleIds = new String[1];

   // Sales Manager role name
   String strRoleName = "Sales Manager";
   String strErrorMsg;

   try
   {
      // Get the UserAuth of the currently logged on user
      userAuth = bizUser.WhoAmI();
   
      // Get the ID of the Sales Manager role in the caller's business
      strRoleIds[0] = secRole.RetrieveIdFromName(userAuth, 
                                  userAuth.MerchantId, strRoleName);

      // Assign the Sales Manager role to the caller user
      secRole.AssignUserRoles(userAuth, userAuth.UserId, strRoleIds);
   }
   catch (System.Web.Services.Protocols.SoapException err)
   {
      // Process the platform error here
      strErrorMsg = String.Concat("ErrorMessage: ", err.Message, " ",
                      err.Detail.OuterXml, " Source: ", err.Source);
   }
}


© 2005 Microsoft Corporation. All rights reserved.


Show:
© 2014 Microsoft