CRM 2011: change format of textfield to hyperlink

A customer asked me to change a single line text field to a hyperlink. As we know or as you have found out. It’s not possible to change the formatting of fields. Personally I think this is something which should be available. Maybe something for the next Dynamics CRM release?

Anyway, I had some options

1) Create a new field with hyperlink formatting, make sure the data is copied from the old field to the new, delete the old field  -> Too much work

2) Create a new field with hyperlink formatting, create a jscript that copies over the data –> Not too much work, but there has to be something else, right?

After some googling I found this URL: http://mscrmkb.blogspot.be/2012/01/convert-text-field-to-link-button-to.html (ALL the credits go to this person, this blogpost is more like a personal note).

This script will convert your field to a link button, how cool is that? Here is the script I used:

function ConvertToLink(fldName) {
    if(Xrm.Page.getAttribute(fldName).getValue()!=null)
    {
        var content = Xrm.Page.getAttribute(fldName).getValue();
        var btn = "<a href='javascript: void(0);' onclick=\"window.open(\'"+content+"',
        \'windowname1\', \'width=600, height=650\');  return false;\" style='color:blue;text-decoration:
        underline !important'>"+content+"</a>";
        var ctrl = Xrm.Page.ui.controls.get(fldName)._control;
        // Add the new button 
        ctrl.get_element().innerHTML += btn;
        // Hide the textbox 
        ctrl.get_element().firstChild.style.display = 'none';
    }
}

Get optionset text from value or value from text

Here are two functions I use on a regular base when working with optionsets in CRM 2011 development projects. When you request an optionset attribute you will get an object of the type “OptionSetValue”. This object contains the value of the selected item. Sometimes this is enough but sometimes you want to retrieve the textual value behind this numeric value. Or sometimes you need to set a certain value in an optionset based on a value. If you then only have the text value, then you need a way to retrieve the numeric value behind that text value. For these common problems I have two methods:

/// <summary>
/// This function is used to retrieve the optionset value using the optionset text label
/// </summary>
/// <param name="service"></param>
/// <param name="entityName"></param>
/// <param name="attributeName"></param>
/// <param name="selectedLabel"></param>
/// <returns></returns>
private int GetOptionsSetValueForLabel(IOrganizationService service, string entityName, string attributeName, string selectedLabel)
{

    RetrieveAttributeRequest retrieveAttributeRequest = new
    RetrieveAttributeRequest
    {
        EntityLogicalName = entityName,
        LogicalName = attributeName,
        RetrieveAsIfPublished = true
    };
    // Execute the request.
    RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
    // Access the retrieved attribute.
    Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)
    retrieveAttributeResponse.AttributeMetadata;// Get the current options list for the retrieved attribute.
    OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
    int selectedOptionValue = 0;
    foreach (OptionMetadata oMD in optionList)
    {
        if (oMD.Label.LocalizedLabels[0].Label.ToString().ToLower() == selectedLabel.ToLower())
        {
            selectedOptionValue = oMD.Value.Value;
            break;
        }
    }
    return selectedOptionValue;
}
/// <summary>
/// This function is used to retrieve the optionset labek using the optionset value
/// </summary>
/// <param name="service"></param>
/// <param name="entityName"></param>
/// <param name="attributeName"></param>
/// <param name="selectedValue"></param>
/// <returns></returns>
private string GetOptionsSetTextForValue(IOrganizationService service, string entityName, string attributeName, int selectedValue)
{

    RetrieveAttributeRequest retrieveAttributeRequest = new
    RetrieveAttributeRequest
    {
        EntityLogicalName = entityName,
        LogicalName = attributeName,
        RetrieveAsIfPublished = true
    };
    // Execute the request.
    RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
    // Access the retrieved attribute.
    Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)
    retrieveAttributeResponse.AttributeMetadata;// Get the current options list for the retrieved attribute.
    OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
    string selectedOptionLabel = null;
    foreach (OptionMetadata oMD in optionList)
    {
        if (oMD.Value == selectedValue)
        {
            selectedOptionLabel = oMD.Label.LocalizedLabels[0].Label.ToString();
            break;
        }
    }
    return selectedOptionLabel;
}

Iterating over requiredattendees in an appointment

In this sample code I will show you how to iterate over the requiredattandees attribute of an appointment. This  attribute is a list of activityparty records. An activityparty record can contain many different entity types, for example contacts and accounts:

proxy.EnableProxyTypes();
//If you want to iterate over every appointment use a RetrieveMumltipleRequest
var appointment = proxy.Retrieve(Appointment.EntityLogicalName,new Guid("FC8C427B-26AE-E111-ADAD-000C29CBD80B"),new ColumnSet(true));
List<ActivityParty> party = ((Appointment)appointment).RequiredAttendees.ToList<ActivityParty>();
foreach (ActivityParty ap in party)
{
    //You can retrieve metadata about a contact or an account by performing a new request
    if(ap.PartyId.LogicalName == Contact.EntityLogicalName)
        Console.WriteLine(string.Format("It's a contact with id {0}",ap.PartyId.Id));
    else if (ap.PartyId.LogicalName == Account.EntityLogicalName)
        Console.WriteLine(string.Format("It's an account with id {0}", ap.PartyId.Id));
}

Project setup for CRM 2011 custom workflow activity development

Today I wanted to create a custom workflow activity in code. I had some issues while creating and building this project in Visual Studio.By trial and error I slowly discovered what I was doing wrong. So here is you and for myself as future reference the setup of a CRM 2011 custom workflow activity.

1) Add XRM binaries from CRM 2011 SDK BIN folder. You need to add the following two:
- microsoft.xrm.sdk
- microsoft.xrm.sdk.workflow

2) These two assemblies have dependencies on System.ServiceModel and System.ServiceModel.Web so you need to add these too.
If you can’t add them, make sure that the target framework in you project properties is set to “.NET Framework 4″ and not “.NET Framework 4 Client Profile”

If you don’t add them then you’ll get build errors like:

- The referenced assembly “microsoft.xrm.sdk” could not be resolved because it has a dependency on “System.ServiceModel.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ which is not in the currently targeted framework “.NETFramework,Version=v4.0,Profile=Client”. Please remove references to assemblies not in the targeted framework or consider retargeting your project.
- The referenced assembly “microsoft.xrm.sdk.workflow” could not be resolved because it has a dependency on “System.ServiceModel.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ which is not in the currently targeted framework “.NETFramework,Version=v4.0,Profile=Client”. Please remove references to assemblies not in the targeted framework or consider retargeting your project.

3) Also add a reference to System.Runtime.Serialization if you are going to use an entities class generated by crmsvcutil.exe

Now you are finally set to start coding your custom CRM 2011 Workflow Activity Library…

4) Remove Activity1.xaml

3) Add a new C# class file to contain your custom workflow code

Once the workflow activity library is created…

5) Make sure to sign your assembly via the Project Properties / Signing tab

Custom workflow activity libraries need to be registered like a regular CRM plugin so…

6) Make sure you are a CRM 2011 deployment administrator otherwise you’ll not be able to register the plugin

Use the custom workflow activity in your CRM 2011 processes.

Free SharePoint 2007 Development training sessions

The FireStarter event is a US event where they show as much as possible content on a specific subject. Basically a kind of technical bootcamp. Some time ago there was such an event on SharePoint development. These are 101 courses so a beginner should do fine with these and a more experienced developer can brush up a bit. Are you wondering how you can use Silverlight with SharePoint. Asking yourself the questions: what is a SharePoint webpart, how do I develop a SharePoint webpart. What are webservices? How can I use webservices together with SharePoint? What are Workflow’s? How can I create a workflow process in SharePoint? What kind of events are there in a SharePoint site and how can I catch these events? How can I brand my SharePoint site with my custom colors and logo’s? Well, an answer to all these questions can be found in the  following recordings.

LIVE MEETING REPLAY URL:                       

Session 1 – Introduction To Day & Keynote:

https://www.livemeeting.com/cc/mseventsbmo/view?id=1032380419&role=attend

Session 2 – Whirlwind SharePoint on Visual Studio:

https://www.livemeeting.com/cc/mseventsbmo/view?id=1032380419-1&role=attend

Session 3 – Silverlight on SharePoint:

https://www.livemeeting.com/cc/mseventsbmo/view?id=1032380419-2&role=attend

Session 4 – Web Parts on SharePoint:

https://www.livemeeting.com/cc/mseventsbmo/view?id=1032380419-3&role=attend

Session 5 – Web Services on SharePoint:

https://www.livemeeting.com/cc/mseventsbmo/view?id=1032380419-4&role=attend

Session 6 – Workflow on SharePoint:

https://www.livemeeting.com/cc/mseventsbmo/view?id=1032380419-5&role=attend

Session 7 – Event Handlers on SharePoint:

https://www.livemeeting.com/cc/mseventsbmo/view?id=1032380419-6&role=attend

Session 8 – Page Branding on SharePoint:

https://www.livemeeting.com/cc/mseventsbmo/view?id=1032380419-7&role=attend

 

More info can be found here: SharePoint Web 2.0 FireStarter – Live Meeting Recordings