How to pass list object with ajax request on Asp.net Mvc

Sample controller and action

 [HttpPost]
 public ActionResult UpdateEmployeePerformance(List<ObjectivesList> ObjectivesList, EmployeePerformance Performance)
 {
     return Json("Ok", JsonRequestBehavior.AllowGet);
 }

Sample Ajax request

$.ajax({
	url: "/Custom/UpdateEmployeePerformanceReview",
	type: "POST",
	data: JSON.stringify({ ObjectivesList: updateObjectivesList, EmployeePerformance  : Performance}),
	dataType: "json",
	async: false,
	contentType: "application/json; charset=utf-8",
	beforeSend: function (XMLHttpRequest) {
		XMLHttpRequest.setRequestHeader("Accept", "application/json");
	},
	success: function (data) {
		debugger
	},
	error: function (XMLHttpRequest, textStatus, errorThrown) {
		debugger;
	}
});

#ajax-list-parameters-request, #ajax-request, #asp-net-mvc, #jquery

How to add lookup filter on custom javascripts

This function bind to form onload “OnLoad”, after calling the “preFilterLookup” function. This method access to form control and add to “addPreSearch” events. In events calling the custom fetchxml functions.

 


function OnLoad() {
preFilterLookup();
}

function preFilterLookup() {
Xrm.Page.getControl("formcontrolelementid").addPreSearch(function () {
addLookupFilter();
});
}

function addLookupFilter() {
fetchXml = "<filter type='and'>"
fetchXml += "<condition attribute='logicalname' operator='eq' value='value' ></condition>";
fetchXml += "</filter>";
Xrm.Page.getControl("formcontrolelementid").addCustomFilter(fetchXml);
}

#custom-js, #lookup, #lookup-filter, #web-resource

How to get global option set values for dynamics crm with c#

This method maybe helping for you


public static List<GlobalOptionSetModel> GetGlobalOptionSetList(string globalOptionSetName, IOrganizationService service)

{

List<GlobalOptionSetModel> result = new List<GlobalOptionSetModel>();

RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest { Name = globalOptionSetName };

RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)service.Execute(retrieveOptionSetRequest);

OptionSetMetadata optionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;

List<OptionMetadata> optionList = optionSetMetadata.Options.ToList();

result = optionList.ConvertAll(z => new GlobalOptionSetModel { Label = z.Label.UserLocalizedLabel.Label, Value = z.Value.ToString(), LanguageCode = z.Label.UserLocalizedLabel.LanguageCode.ToString() });

return result;
}

public class GlobalOptionSetModel
{
public String LanguageCode { get; set; }
public String Value { get; set; }
public String Label { get; set; }
}

 

#csharp-global-option-set, #get-global-option-set-values, #ms-crm-global-option-set-values

Redirect to url after specific time with javascript

Try this


function RedirectUrl(url, interval) {

if (url!= null && url!= '') {

if (interval != undefined && interval != '') {
window.setTimeout(function () {
window.location.href = url;
}, interval);
} else {
window.location.href = url;
}

}

}

#interval, #javascript-url-redirect, #jquery, #settimeout, #url-redirect

How to include javascript file in html dynamically

Try this.


$.ajax({
url: 'library.js',
dataType: 'script',
async: false, //if you change this true, you may receive the error
success: function () {

}
});

#ajax, #dynamic-javascript, #dynamics-library

How to get picklist option value with localized labels

Sometimes we use in the system can have multiple language options.
In such a situation it may be necessary to show the information in the interface according the to the language options.
Picklist was appropriate in this case we will see how to use multiple languages.

 


//Creating crm service

CrmServiceClient crmSvc = null;
crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("***", "***", "***"), AuthenticationType.AD, "***", "443", "***", useUniqueInstance: false, useSsl: true);

string logicalName = "my_logicalname";
string entityLogicalName = "my_entityname";

//Get metadata for localization

EntityMetadata entityMetadata = crmSvc.GetEntityMetadata(entityLogicalName, EntityFilters.All);
AttributeMetadata attribute = entityMetadata.Attributes.FirstOrDefault(z => z.LogicalName.Equals(logicalName));

if (attribute!= null && attribute.AttributeType == AttributeTypeCode.Picklist)
{
var options = (((Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)(attribute))).OptionSet.Options;
List<PickList> picklist = new List<PickList>();

picklist = options.ToList().ConvertAll(z => new PickList {
Label = z.Label.UserLocalizedLabel.Label,
Value = z.Value.Value.ToString()
});
}

public class PickList
{
public String DefaultValue { get; set; }
public String Value { get; set; }
public String Label { get; set; }
}

Another way

RetrieveEntityRequest rer = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.Attributes,
LogicalName ="entitylogicalname"
};

RetrieveEntityResponse reres = (RetrieveEntityResponse)service.Execute(rer);

AttributeMetadata attribute = reres.EntityMetadata.Attributes.FirstOrDefault(z => z.LogicalName.Equals("logicalname"));

if (attribute != null && attribute.AttributeType == AttributeTypeCode.Picklist)
{
 var options = (((Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)(attribute))).OptionSet.Options;
 List<MyClass> picklist = new List<MyClass>();

 picklist = options.ToList().ConvertAll(z => new MyClass
 {
 Text = z.Label.UserLocalizedLabel.Label,
 Value = z.Value.Value.ToString()
 });

}

 

 

#csharp-picklist, #dynamaics-crm-localization, #picklist, #user-localized-label

C# builder pattern

C# builder pattern sample


class Program
{
static void Main(string[] args)
{
string[] partlist1 = { "a", "b", "c" };
string[] partlist2 = { "1", "2", "3" };

Builder builder = new ConcreateClass1();
Director.Construct(builder, partlist1);
Product product1 = builder.GetProduct();
Console.WriteLine(product1.ToString());

builder = new ConcreateClass2();
Director.Construct(builder, partlist2);
Product product2 = builder.GetProduct();
Console.WriteLine(product2.ToString());

Console.ReadLine();
}
}

class Director
{
public static void Construct(Builder builder, string[] parts)
{
parts.ToList().ForEach(z => {
builder.BuildPart(z);
});
}
}

public abstract class Builder
{
public abstract void BuildPart(string partName);
public abstract Product GetProduct();
}

public class ConcreateClass1 : Builder
{

private Product part = new Product();

public override void BuildPart(string partName)
{
part.Add(partName);
}

public override Product GetProduct()
{
return part;
}
}

public class ConcreateClass2 : Builder
{

private Product part = new Product();

public override void BuildPart(string partName)
{
part.Add(partName);
}

public override Product GetProduct()
{
return part;
}
}

public class Product
{
private List<string> _parts = new List<string>();

public void Add(string part)
{
this._parts.Add(part);
}

public override string ToString()
{
string parts = string.Empty;
_parts.ForEach(z =>
{
parts += " " + z;
});

return String.Format("Part list {0}", parts);
}
}

#builder-pattern, #csharp-pattern