Adding a contact picture

Based on some of the previous examples, this recipe shows a very basic way to reference a contact picture from an external source. This source could be anything from an internal repository to any public image hosting site. Also, you can use the same approach to load images to any entity. In the following recipes, we will delve deeper into referencing images from within the system.

Getting ready

For this example, we will be using an existing solution. Alternatively, we can create a new solution. We will customize the Contact entity.

How to do it...

  1. Add the Contact entity to your solution if not already added.
  2. Add a new web resource of type JScript.
  3. Add the following function to your entity:
    function AddContactPicture()
    {
      var _imageURL = Xrm.Page.getAttribute("new_profileimage").getValue();
      Xrm.Page.getControl("WebResource_ContactPicture").setSrc(_imageURL);
    }
  4. Open the main Contact form for editing.
  5. Add a new Web Resource element to your form. Name it WebResource_ContactPicture.
  6. In the Web resource lookup, point to a generic image as a base, in case no user image is provided.
  7. Format it, so it looks right on the Contact form, by using the configurations on the Formatting tab.
  8. Add a new Text field to the form, and format it as a URL. Name it new_profileimage.
  9. Attach the AddContactPicture() function to the OnChange event of the previously created text field, as well as to the OnLoad event of the form.
  10. Save and Close the form.
  11. Publish your solution.
  12. Test it by opening a new contact. On the first load, since no image URL is defined, the default image will be shown.
  13. Add a URL to an image hosted on a public site.
  14. Save the form. This will cause a form refresh. The image we've defined in the URL will now show in the image form element.
    How to do it...

How it works...

The defined script loads a URL to an image and places that image in our form-defined web control. Formatting the text field as URL adds the necessary validation on the field, so we don't end up with unexpected user input.

There's more...

While using an image from an external source is quite easy using this approach, it's not necessarily a good idea to take this approach. You could consider keeping the image with the record. Take a look at the next recipe for an example on how to do that.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.2.223