Saturday, March 17, 2012

Win 8 consumer preview

installed windows 8 consumer preview..... First look has the wow feeling!!!!!!!

Tuesday, March 13, 2012

Read Configuration file (Web resource - Data (Xml)...

Read Configuration file (Web resource - Data (Xml)...: Sometimes, we would like to read data of configuration file which has been added as web resource of type Data (Xml) in Dynamic CRM. In t...

Tuesday, February 28, 2012

Get User Security Roles in Jscript


function GetAllSystemRoles() {
    var serverUrl = Xrm.Page.context.getServerUrl();

    var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
    oDataEndpointUrl += "RoleSet";
    var service = GetRequestObject();
    if (service != null) {
   
    service.open("GET", oDataEndpointUrl, false);
    service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
    service.setRequestHeader("Accept", "application/json, text/javascript, */*");
    service.send(null);

    var requestResults = eval('(' + service.responseText + ')').d;
    return requestResults;
    }
}


function GetRoleIdByName(allsecurityRoles,rolename) {
    if (allsecurityRoles != null && allsecurityRoles.results.length > 0) {
        for (var i = 0; i < allsecurityRoles.results.length; i++) {
            var role = allsecurityRoles.results[i];
            if (role.Name == rolename) {
                id = role.RoleId;
                return id;
            }

        }
    }
    return null;
}

function UserHasRole(allsecurityRoles) {
    debugger;
    var nsrApprovedPrivilage = new Array("Pricing Analyst", "TSC", "Pricing Team manager", "TSC Manager", "Sales Manager - Direct", "Sales Manager - Telesales", "Sales Manager - Winback", "General Manager - Direct", "TSM");
    for (var j = 0; j < nsrApprovedPrivilage.length; j++) {
        var id = GetRoleIdByName(allsecurityRoles, nsrApprovedPrivilage[j]);
        var currentUserRoles = Xrm.Page.context.getUserRoles();
        for (var i = 0; i < currentUserRoles.length; i++) {
            var userRole = currentUserRoles[i];
            if (GuidsAreEqual(userRole, id)) {
                return true;
            }
        }
    }
   return false;
}

function GetRequestObject() {
    if (window.XMLHttpRequest) {
        return new window.XMLHttpRequest;
    }
    else {
        try {
            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch (ex) {
            return null;
        }
    }
}

function GuidsAreEqual(guid1, guid2) {
    var isEqual = false;

    if (guid1 == null || guid2 == null) {
        isEqual = false;
    }
    else {
        isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
    }

    return isEqual;
}



function CheckIfUserCanApprove() {
    debugger;
    var hasPrivilagesToApprove = UserHasRole(GetAllSystemRoles());
    if (hasPrivilagesToApprove) {
        alert("hasPrivilagesToApprove");
    }
    else {
        alert("does not have hasPrivilagesToApprove");
    }
}


Add the list of security roles in nsrApprovedPrivilage array to check if user has that particular role

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;
}