Integrating CRM 2011 Online and SharePoint 2013 Online using BCS oData Proxy


Here we will be creating an ASP.NET Data Service and host it in azure. This ASP.NET Data Service will be used to generate BCS Model in SharePoint 2013 online. The service will allow basic CRUD operation on Contact record in CRM 2011 from SharePoint 2013 online.

  • Create a new Windows Azure Cloud Service project in VS 2012

  • Select WCF Service Web Role

  • Remove the System.Data.Services.Client.dll from the References

  • Add a new item ASP.NET Data Service to the WCFServiceWebRole1 project


  • Add a new class to the project

  • Specify the class name in the ASP.NET Data Service class

  • Add references to the following dll’s
  1. Microsoft.crm.sdk.proxy
  2. Microsoft.xrm.sdk
  3. Microsoft.IdentityModel
  • Set Copy Local as true for the Micrsoft.IdentityModel

  • Source Code for CrmContext.cs, for CRUD Operation (we need to implement IUpdatable Interface for CRUD)
</pre>
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Web;
namespace WCFServiceWebRole1
{
 [DataServiceKey("ContactId")]
 [Serializable]
 public class Contact
 {
 [DataMemberAttribute()]
 public Guid ContactId
 {
 get;
 set;
 }
 [DataMemberAttribute()]
 public string FirstName
 {
 get;
 set;
 }
 [DataMemberAttribute()]
 public string LastName
 {
 get;
 set;
 }
 }

public class CrmContext : IUpdatable
 {
 public static List<Contact> lstContacts;
 public static OrganizationServiceProxy orgProxy = GetOrganizationService();

public static List<Contact> GetContacts()
 {
 List<Contact> lstContact = new List<Contact>();
 QueryExpression queryExpression = new QueryExpression("contact");
 ConditionExpression conditionExpression = new ConditionExpression("statuscode", ConditionOperator.Equal, 1);
 queryExpression.ColumnSet = new ColumnSet(new String[] { "contactid", "firstname", "lastname" });
 EntityCollection contactCollection = orgProxy.RetrieveMultiple(queryExpression);

Contact contactRecord;
 foreach (Entity entity in contactCollection.Entities)
 {
 contactRecord = new Contact();
 contactRecord.ContactId = (Guid)entity.Attributes["contactid"];
 if (entity.Contains("firstname"))
 {
 contactRecord.FirstName = entity.Attributes["firstname"].ToString();
 }
 if (entity.Contains("lastname"))
 {
 contactRecord.LastName = entity.Attributes["lastname"].ToString();
 }

lstContact.Add(contactRecord);
 }

return lstContact;
 }

public IQueryable<Contact> Contacts
 {
 get
 {
 return lstContacts.AsQueryable();
 }
 }

Contact currentEntry;
 bool NewEntry = false;
 bool Delete = false;

#region implemented methods
 object IUpdatable.CreateResource(string containerName, string fullTypeName)
 {
 var objType = Type.GetType(fullTypeName);
 var resourceToAdd = Activator.CreateInstance(objType);
 lstContacts.Add((Contact)resourceToAdd);
 currentEntry = (Contact)resourceToAdd;
 NewEntry = true;
 return resourceToAdd;
 }

void IUpdatable.DeleteResource(object targetResource)
 {
 orgProxy.Delete("contact", ((Contact)targetResource).ContactId);
 lstContacts.Remove(targetResource as Contact);
 Delete = true;
 }

object IUpdatable.GetResource(IQueryable query, string fullTypeName)
 {
 object result = null;
 var enumerator = query.GetEnumerator();
 while (enumerator.MoveNext())
 {
 if (enumerator.Current != null)
 {
 result = enumerator.Current;
 break;
 }
 }
 if (fullTypeName != null && !fullTypeName.Equals(result.GetType().FullName))
 {
 throw new DataServiceException();
 }

return result;
 }

object IUpdatable.GetValue(object targetResource, string propertyName)
 {
 return targetResource.GetType().GetProperty(propertyName).GetValue(targetResource, null);

}

object IUpdatable.ResolveResource(object resource)
 {
 return resource;
 }

void IUpdatable.SaveChanges()
 {
 if (!Delete)
 {
 if (!NewEntry)
 {
 Entity contactNewRecord = new Entity();
 contactNewRecord.LogicalName = "contact";
 contactNewRecord.Attributes["contactid"] = currentEntry.ContactId;
 contactNewRecord.Attributes["firstname"] = currentEntry.FirstName;
 contactNewRecord.Attributes["lastname"] = currentEntry.LastName;
 orgProxy.Update(contactNewRecord);
 }
 else if (NewEntry)
 {
 Entity contactUpdateRecord = new Entity();
 contactUpdateRecord.LogicalName = "contact";
 contactUpdateRecord.Attributes["contactid"] = currentEntry.ContactId;
 contactUpdateRecord.Attributes["firstname"] = currentEntry.FirstName;
 contactUpdateRecord.Attributes["lastname"] = currentEntry.LastName;
 orgProxy.Create(contactUpdateRecord);
 }
 }

}

void IUpdatable.SetValue(object targetResource, string propertyName, object propertyValue)
 {
 Type TargetType = targetResource.GetType();
 PropertyInfo property = TargetType.GetProperty(propertyName);
 property.SetValue(targetResource, propertyValue, null);
 currentEntry = targetResource as Contact;
 }
 #endregion

#region not implemented methods

void IUpdatable.RemoveReferenceFromCollection(object targetResource, string propertyName, object resourceToBeRemoved)
 {
 throw new NotImplementedException();
 }

void IUpdatable.AddReferenceToCollection(object targetResource, string propertyName, object resourceToBeAdded)
 {
 throw new NotImplementedException();
 }

void IUpdatable.ClearChanges()
 {
 throw new NotImplementedException();
 }
 object IUpdatable.ResetResource(object resource)
 {
 return resource;
 }
 void IUpdatable.SetReference(object targetResource, string propertyName, object propertyValue)
 {
 throw new NotImplementedException();
 }
 #endregion
 public static OrganizationServiceProxy GetOrganizationService()
 {

 IServiceManagement<IOrganizationService> orgServiceManagement =
 ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(ConfigurationManager.AppSettings["OrganizationService"]));
 AuthenticationCredentials authCredentials = new AuthenticationCredentials();
 authCredentials.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["UserName"];
 authCredentials.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["Password"];
 AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);
 return new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);

}

}
}
<pre>

Source Code for CRMDataService.svc


using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;

namespace WCFServiceWebRole1
{
 public class CrmContactService : DataService<CrmContext>
 {
 // This method is called only once to initialize service-wide policies.
 public static void InitializeService(DataServiceConfiguration config)
 {
 config.SetEntitySetAccessRule("*", EntitySetRights.All);
 config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
 config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
 }

protected override CrmContext CreateDataSource()
 {
 CrmContext.lstContacts = CrmContext.GetContacts();
 return base.CreateDataSource();
 }
 }
}

  • Build and Publish the service to Azure

  • Test the service in browser

  • Create a new SharePoint App project in Visual Studio

  • Right click the App Project and select the option “Content Types for an External Data Source”

  • Specify the URL for the WCF Data Service and give a Data Source Name and select Next

  • Select the Contacts data entity

  • Deploy the app

  • Get the URL where the app is installed

  • Append the External List path to the URL

  • The Contact records will be displayed in the list in SharePoint.

  • The list item also support update and delete operation

Hope it helps.

This organization name contains unsupported characters or reserved names error while creating new organization using Deployment Manager in CRM 2011


Hi,

I got below error when I was trying to create a new organization named dev. Dev is one the reserved names which cannot be used.

There is a table named ReservedNames in MSCRM_CONFIG database that lists all the reserved names.

  1. About
  2. Activities
  3. AdvancedFind
  4. api
  5. AppWebServices
  6. aspnet_client
  7. bi
  8. bin
  9. Biz
  10. Calendar
  11. Condition
  12. CRMReports
  13. CS
  14. dev
  15. Help
  16. Home
  17. Import
  18. MA
  19. MSCRMServices
  20. Notes
  21. Products
  22. rc
  23. Reports
  24. ReportServer
  25. ResourceCenter
  26. Sdk
  27. SFA
  28. signin
  29. SM
  30. support
  31. Tools
  32. tsweb
  33. UnitTests
  34. UserDefined
  35. Workplace

The helpful post

http://www.mscrmking.com/2012/04/list-the-reserved-word/

Hope it helps.

Hiding Activities and Closed Activities from left navigation pane of form in CRM 2011


Hi,

I was struggling hard to hide the Activities and Closed Activities from the form’s left navigation pane. Then came across the post which explained how to achieve the same. Actually in System Entity’s Form we can remove those links using Form Editor, however we cannot do the same for the custom entity’s form. There we can make use of Xrm.Page.ui.navigation class to hide them.

Check out the helpful post

http://www.magnetismsolutions.co.nz/blog/roshan/11-12-15/Hide_%E2%80%9CActivities%E2%80%9D_and_%E2%80%9CClosed_Activities%E2%80%9D_from_Dynamics_CRM_2011_Forms.aspx

Bye.

Programmatically working with Service Scheduling in CRM


Hi,

Please check out the following post that explains in detail the concept of Service Scheduling programmatically.

Calendar, Inner Calendar etc..

http://blogs.msdn.com/b/crm/archive/2008/02/15/service-scheduling-exposed-partially.aspx

This is the other helpful post

http://dotnetdevlife.blogspot.com/2011/07/crm-2011-creating-service-restrictions.html

Hope it helps.

Programmatically set Time Off for the System User in CRM 2011


We can make use of below sample code to set the Time Off for the user programmatically. In the below code we are setting time off of duration 8 hours on 7th May 2013.


IServiceManagement<IOrganizationService> orgServiceManagement =
 ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri("https://orgname.crm5.dynamics.com/XRMServices/2011/Organization.svc"));

AuthenticationCredentials authCredentials = new AuthenticationCredentials();
 authCredentials.ClientCredentials.UserName.UserName = "user@domain.onmicrosoft.com";
 authCredentials.ClientCredentials.UserName.Password = "password";
 AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);

OrganizationServiceProxy organizationProxy = new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);

 // Get the user id
 Guid userid = ((WhoAmIResponse)organizationProxy.Execute(new WhoAmIRequest())).UserId;

// Get the calendar id of the user
 Entity systemUserEntity = organizationProxy.Retrieve("systemuser", userid, new ColumnSet(new String[] { "calendarid"}));

// Retrieve the calendar of the user
 Entity userCalendarEntity = organizationProxy.Retrieve("calendar", ((Microsoft.Xrm.Sdk.EntityReference)(systemUserEntity.Attributes["calendarid"])).Id, new ColumnSet(true));

// Retrieve the calendar rules defined in the calendar
 EntityCollection calendarRules = (EntityCollection)userCalendarEntity.Attributes["calendarrules"];

// Create a new inner calendar
 Entity newInnerCalendar = new Entity("calendar");
 newInnerCalendar.Attributes["businessunitid"] = new EntityReference("businessunit", ((Microsoft.Xrm.Sdk.EntityReference)(userCalendarEntity["businessunitid"])).Id);
 Guid innerCalendarId = organizationProxy.Create(newInnerCalendar);

// Create a new calendar rule and assign the inner calendar id to it
 Entity calendarRule = new Entity("calendarrule");
 calendarRule.Attributes["description"] = "Time Off Rule";
 calendarRule.Attributes["duration"] = 480;
 calendarRule.Attributes["extentcode"] = 2;
 calendarRule.Attributes["pattern"] = "FREQ=DAILY;INTERVAL=1;COUNT=1";
 calendarRule.Attributes["rank"] = 0;
 calendarRule.Attributes["timezonecode"] = 190;
 calendarRule.Attributes["innercalendarid"] = new EntityReference("calendar", innerCalendarId);

// starting at 12:00 on 7 May
 calendarRule.Attributes["starttime"] = new DateTime(2013, 5, 7,0,0,0,DateTimeKind.Utc);
 calendarRules.Entities.Add(calendarRule);

// assign all the calendar rule back to the user calendar
 userCalendarEntity.Attributes["calendarrules"] = calendarRules;
 // update the user calendar entity that has the new rule
 organizationProxy.Update(userCalendarEntity);

Entity calendarRule1 = new Entity("calendarrule");

// duration of 8 hours
 calendarRule1.Attributes["duration"] = 480;
 calendarRule1.Attributes["effort"] = 2.0;
 calendarRule1.Attributes["issimple"] = true;


 calendarRule1.Attributes["offset"] = 0;
 calendarRule1.Attributes["rank"] = 0;
 // subcode 6= vacation
 calendarRule1.Attributes["subcode"] = 6;
 // time code 2 = unavailable
 calendarRule1.Attributes["timecode"] = 2;
 calendarRule1.Attributes["timezonecode"] = -1;
 calendarRule1.Attributes["calendarid"] = new EntityReference("calendar", innerCalendarId);

EntityCollection innerCalendarRules = new EntityCollection();
 innerCalendarRules.EntityName = "calendarrule";
 innerCalendarRules.Entities.Add(calendarRule1);

newInnerCalendar.Attributes["calendarrules"] = innerCalendarRules;
 newInnerCalendar.Attributes["calendarid"] = innerCalendarId;
 organizationProxy.Update(newInnerCalendar);

Hope it helps

Filter on Add Existing Lookup for 1-n relationship in CRM 2011.


Hi,

We recently had a requirement to implement filtering on Add Existing lookup button for 1-n relationship.

This is the post that helped us to implement it.

http://sliong.wordpress.com/2012/06/22/crm-2011-filtered-lookup-for-add-existing-button-for-one-to-many-relationship-crmformsubmit-is-undefined/

Only change was in the name of the getParameter function.

function
replaceAddExistingButtonView(params) {

    var
relName = params.gridControl.getParameter(“relName”),

    var roleOrd = params.gridControl.getParameter(“roleOrd”),

instead of getParameter it is now GetParameter (capital G)

Bye.