Monday, February 20, 2012

CRM 2011 Convert EntityCollection to DataTable


public DataTable convertEntityCollectionToDataTable(EntityCollection BEC)
        {
            DataTable dt = new DataTable();
            int total = BEC.Entities.Count;
            for (int i = 0; i < total; i++)
            {
                DataRow row = dt.NewRow();
                Entity myEntity = (Entity)BEC.Entities[i];
                var keys= myEntity.Attributes.Keys;
                foreach (var item in keys)
           {
                    string columnName = item;
                    string value = getValuefromAttribute(myEntity.Attributes[item]);
                    if (dt.Columns.IndexOf(columnName) == -1)
                    {
                        dt.Columns.Add(item, Type.GetType("System.String"));
                    }
                    row[columnName] = value;
                }
                dt.Rows.Add(row);
            }
            return dt;
        }

        private string getValuefromAttribute(object p)
        {
            if (p.ToString() == "Microsoft.Xrm.Sdk.EntityReference")
            {
                return ((EntityReference)p).Name;
            }
            if (p.ToString() == "Microsoft.Xrm.Sdk.OptionSetValue")
            {
                return ((OptionSetValue)p).Value.ToString();
            }
            if (p.ToString() == "Microsoft.Xrm.Sdk.Money")
            {
                return ((Money)p).Value.ToString();
            }
            if (p.ToString() == "Microsoft.Xrm.Sdk.AliasedValue")
            {
                return ((Microsoft.Xrm.Sdk.AliasedValue)p).Value.ToString();
            }
            else
            {
                return p.ToString();
            }
        }

Tuesday, February 7, 2012

code that can retrieve total number of your desired entity



 code that can retrieve total number of your desired entity and return this as int
///

/// Gets the supplied entity count from CRM 2011.
///

/// "Service">The CRM 2011 service.
/// "EntityName">Name of the entity we need get count of.
/// "ServicePageSize">Size of the page (optional).
///
private int GetEntityCount(IOrganizationService Service, string EntityName, int ServicePageSize = 5000)
{
RetrieveMultipleResponse retrieved;
int PageNumber = 1;
string PagingCookie = string.Empty;
int PageSize = ServicePageSize;

do {
QueryExpression query = new QueryExpression() {
EntityName = EntityName,
//ColumnSet = new ColumnSet(Columns),
PageInfo = new PagingInfo() {
PageNumber = 1,
Count = PageSize
}
};

if (PageNumber != 1) {
query.PageInfo.PageNumber = PageNumber;
query.PageInfo.PagingCookie = PagingCookie;
}

RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
retrieve.Query = query;
retrieved = (RetrieveMultipleResponse)Service.Execute(retrieve);

if (retrieved.EntityCollection.MoreRecords) {
PageNumber++;
PagingCookie = retrieved.EntityCollection.PagingCookie;
}
} while (retrieved.EntityCollection.MoreRecords);

return ((PageNumber - 1) * PageSize) + retrieved.EntityCollection.Entities.Count;
}

Monday, February 6, 2012

Get User Security Roles


private EntityCollection GetUserSecurityRole(Guid userGuid, IOrganizationService service)
        {
            var query = new QueryExpression
            {
                LinkEntities =
                                {
                                new LinkEntity
                                {
                                    LinkFromEntityName = "role",
                                    LinkFromAttributeName = "roleid",
                                    LinkToEntityName = "systemuserroles",
                                    LinkToAttributeName = "roleid",
                                    LinkCriteria = new FilterExpression
                                        {
                                            FilterOperator =LogicalOperator.And,
                                            Conditions =
                                            {
                                            new ConditionExpression
                                            {
                                            AttributeName =  "systemuserid",
                                            Operator =    ConditionOperator.Equal,
                                            Values =
                                                        {
                                                        userGuid
                                                        }
                                            }
                                        }
                                }
                                }
                                },
                ColumnSet = new ColumnSet(true),
                EntityName = "role"
            };
            var userRoles = service.RetrieveMultiple(query);

            return userRoles;
        }

Monday, January 23, 2012

How to get access privilages for a user on a particular record

            RetrievePrincipalAccessRequest req = new RetrievePrincipalAccessRequest();

            //specify team or systemuser you want to get privileges for
            req.Principal = new EntityReference("team", teamId);

            //specify the CRM record you want to check principal permissions for (can be any entity type)
            req.Target = new EntityReference("account", accountid);

            RetrievePrincipalAccessResponse resp = (RetrievePrincipalAccessResponse)service.Execute(req);
            return resp.AccessRights.ToString();

Tuesday, October 18, 2011

CRM 2011 Get OptionSetValue Label Text

In CRM 2011, when you try to get the value of an OptionSet, you will always get an Integer value instead of the label value.

Below is the Helper method which is used to retrieve the label of the selected option set

public static string GetOptionSetValueLabel(IOrganizationService service, Entity entity, string attribute, OptionSetValue option)
{
string optionLabel = String.Empty;

RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = entity.LogicalName,
LogicalName = attribute,
RetrieveAsIfPublished = true
};

RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)service.Execute(attributeRequest);
AttributeMetadata attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata;
PicklistAttributeMetadata picklistMetadata = (PicklistAttributeMetadata)attrMetadata;

// For every status code value within all of our status codes values
// (all of the values in the drop down list)
foreach (OptionMetadata optionMeta in
picklistMetadata.OptionSet.Options)
{
// Check to see if our current value matches
if (optionMeta.Value == option.Value)
{
// If our numeric value matches, set the string to our status code
// label
optionLabel = optionMeta.Label.UserLocalizedLabel.Label;
}
}

return optionLabel;
}

Thursday, September 15, 2011

CRM 2011 Developer toolkit

CRM 2011 Developer toolkit is awesome!!!!! cuts down lot of development/deployment time.

Recommend everyone to use it.

P.S :- Still has some minor known issues as of August 2011