PreEntity and PostEntity Images in CRM 4.0


Hi ,

When writing callouts for CRM 3.0, we were provided with PreEntityImageXml and PostEntityImageXml as a parameter to methds being overrided by us.

We could specify the fields for the same in the callout.config as prevalue and postvalue tag.

But the things have changed a bit in CRM 4.0.

Here we have to implement the Execute method found in the interface IPlugin.

public void Execute(IPluginExecutionContext context)

Here first we will register our assembly using Plugin Registration Tool

Than we will register a new step and specify the message against which we want to run our plugin.

After this comes the step where we will Register  new image.

In Register New Image dialog box

We can specify Pre Image and Post Image depending upon pre or post event. (Only for post event we can have both pre and post image)

Parameters -Here we can either specify certain fields or can select all attributes.

Entity Alias – Give a name to our image which we will refer in our code. (eg. say we gave LeadImage)

Now to access these values within our plugin we need  to do the following:-

Cast it into a DynamicEntity
DynamicEntity preLead = (DynamicEntity)context.PreEntityImages[“LeadImage”];
DynamicEntity postLead = (DynamicEntity)context.PostEntityImages[“LeadImage”];

For getting the id of the entity ( unlike crm 3.0 we don;t have entity context over here )

Key keyLeadId = (Key)postLead.Properties[“leadid”];
string leadId = keyLeadId.Value.ToString();

For lookup field use this line of code

Lookup lkpLobPre = (Lookup)preLead.Properties[“new_linesofbusinessid”];
strPreLobGuid = lkpLobPre.Value.ToString();
Lookup lkpLobPost = (Lookup)postLead.Properties[“new_linesofbusinessid”];
strPostLobGuid = lkpLobPost .Value.ToString();

For owner field

Owner ownerLead = (Owner)postLead.Properties[“ownerid”];
strPostOwnerGuid = ownerLead.Value.ToString();

For money field

CrmMoney estimatedvalue = (CrmMoney)postLead[“new_expectedrevenue”];
strExpectedRevenue = estimatedvalue.Value.ToString();

For string field

String geoName = (String)postLead[“new_geographyidname”];

For picklist field

Picklist leadqualitycode = (Picklist)postLead[“leadqualitycode”];
strRating = leadqualitycode.name;

Bye

Unable to cast object of type ‘Microsoft.Crm.Sdk.Moniker’ to type ‘Microsoft.Crm.Sdk.DynamicEntity’


I was writing my first callout ( plugin) for CRM 4.0 when i recieved this error.

I realized that this was because of this line of code in my plugin

public void Execute(IPluginExecutionContext context)
{

DynamicEntity   entity = (DynamicEntity)context.InputParameters.Properties[“Target”];

……

For Create and Update message there was no problem.

Problem started coming when i registered the step for Assign message.

So modified the code as following

DynamicEntity entity = null;
if (context.InputParameters.Properties.Contains(“Target”) &&
context.InputParameters.Properties[“Target”] is DynamicEntity)
{
// Obtain the target business entity from the input parmameters.
entity = (DynamicEntity)context.InputParameters.Properties[“Target”];
}

For Assign Message used the following line of code

if (context.MessageName == “Assign”)
{
Moniker myMoniker = null;
if (context.InputParameters.Properties.Contains(“Target”) &&
context.InputParameters.Properties[“Target”] is Moniker)
{
// Obtain the target business entity from the input parmameters.
myMoniker = (Moniker)context.InputParameters.Properties[“Target”];
}

myMoniker.Name –> Gave the name of the entity

myMoniker.Id –> Gave the id of the entity

Bye

Retrieving Members of Team – CRM


Hi,

We can make use of the following function for retrieving the members of a particular team

It takes as parameter instance of a ICrmService(CRM 4.0) or CrmService (CRM 3.0) and the guid of the team and returns an arraylist of of all the members found in the team

public ArrayList GetMembersOfTeam(ICrmService service, String TeamID)
{
// We want the guid of the system users in the team
ColumnSet cols = new ColumnSet(new String[] { “systemuserid” });
// We need to make use of RetrieveMembersTeamRequest class for that
RetrieveMembersTeamRequest rmtRequest = new RetrieveMembersTeamRequest();
// Converting the string TeamID to guid
Guid headGuid = new Guid(TeamID);

rmtRequest.EntityId = headGuid;
rmtRequest.MemberColumnSet = cols;

RetrieveMembersTeamResponse retrieved = (RetrieveMembersTeamResponse)service.Execute(rmtRequest);

// using generic businessEntity list
List<BusinessEntity> sysResult = retrieved.BusinessEntityCollection.BusinessEntities;

// for CRM 3.0
// BusinessEntity[] sysResult = retrieved.BusinessEntityCollection.BusinessEntities;

ArrayList userGuid = new ArrayList();

foreach (BusinessEntity be in sysResult)
{
systemuser user = (systemuser)be;
userGuid.Add(user.systemuserid.Value.ToString());
}
return userGuid;
}

Bye

Calling a asp.net webservice from an asp page


Say we have created a simple web service like this

[WebMethod]
public string Hello(String name) {
return “Hello “+name ;
}

Now we want to call the web service from inside our asp page,

for this we can make use of the following code

<%
Dim myPostData
Dim name
name=”Mickey Mouse”
myPostData=”name=” & name
Dim xmlhttp
Dim postUrl
postUrl = “http://servername/MyService/Service.asmx/Hello&#8221;
Set xmlhttp = server.Createobject(“MSXML2.XMLHTTP”)
xmlhttp.Open “POST”,postUrl,false
xmlhttp.setRequestHeader “Content-Type”,”application/x-www-form-urlencoded”
xmlhttp.send strWebServideDetails
Response.Write(“<b>This is the message i recieved</b>” +xmlhttp.responseText)

%>

Bye

System.Net.WebException the request failed with HTTP status 401: Unauthorized.” Source=”System.Web.Services”


Hi,

I was gettng this error when trying to call the crm’s webservice from my asp.net page

The resolution for this is putting the following code in the web.conig

i.e. enable impersonation and deny anonymous access

<authentication mode=”Windows” />
<identity impersonate=”true”/>
<authorization>
<!–Deny all unauthenticated users–>
<deny users=”?”/>
</authorization
>

Bye

Programmatically uploading and setting permission to a document in a document library in SharePoint


We can use the following code for uploading a document to document library.

// create a filestream object and open an existing file for reading its contents

FileStream oFileStream = File.OpenRead(@”C:\G.doc”);

byte[] content = new byte[oFileStream.Length];

// store the contents in a byte array

oFileStream.Read(content, 0, (int)oFileStream.Length);

oFileStream.Close();

// create an SPSIte object

using (SPSite objSite = new SPSite(“http://d-1246&#8221;))

{

// get reference to the web application or web site

using (SPWeb objWeb = objSite.OpenWeb())

{

// get reference to the document library. Proposal – name of the document library

SPFolder mylibrary = objWeb.Folders[“Proposal”];

objWeb.AllowUnsafeUpdates = true;

// add the file to the document library and update it

SPFile myFile = mylibrary.Files.Add(“MyProposal.doc”, content, true);

mylibrary.Update();

// get the reference to the file just uploaded as a SPListItem for setting permissions

SPListItem myListItem = myFile.Item;

// HasUniqueRoleAssignments – returns false if it inherits permission from parent else true

myListItem.HasUniqueRoleAssignments.ToString());

// BreakRoleInheritance – breaking role inheritance so that we can define our own permissions

myListItem.BreakRoleInheritance(false);

// webroledefinitions – Full Right, Design, Contribute and Read

SPRoleDefinitionCollection webroledefinitions = objWeb.RoleDefinitions;

// group – different groups created in that site

SPGroup group = objWeb.SiteGroups[“MyGroup”];

SPRoleAssignment roleassignment = new SPRoleAssignment(group);

roleassignment.RoleDefinitionBindings.Add(webroledefinitions[“Design”]);

myListItem.RoleAssignments.Add(roleassignment);

// same for the user as well

SPUser user = objWeb.SiteUsers[@”domainname\username”];

SPRoleAssignment roleassignment1 = new SPRoleAssignment(user);

roleassignment1.RoleDefinitionBindings.Add(webroledefinitions[“Full Control”]);

myListItem.RoleAssignments.Add(roleassignment1);

}

}

Bye

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓