//create request to retrieve Webresource
QueryByAttribute requestWebResource = new QueryByAttribute
{
EntityName = WebResource.EntityLogicalName,
ColumnSet = new ColumnSet(true),
};
requestWebResource.Attributes.AddRange("name");
requestWebResource.Values.AddRange("url/XML/TestData.xml");
WebResource webResource = null;
EntityCollection webResourceCollection = organizationService.RetrieveMultiple(requestWebResource);
if (webResourceCollection.Entities.Count == 0)
throw new InvalidPluginExecutionException("Specified Webresource does not exist");
webResource = (WebResource)webResourceCollection.Entities[0];
byte[] binary = Convert.FromBase64String(webResource.Attributes["content"].ToString());
string resourceContent = UnicodeEncoding.UTF8.GetString(binary);
CRM 2011 CS: create request to retrieve Webresource
CRM 2011 CS: Query to get All Records
// Query to get All Records
protected EntityCollection getROBs(string EntityName)
{
CreatOrgSvcProx cosp = new CreatOrgSvcProx();
OrganizationServiceProxy prox = cosp.getServiceProxy();
ColumnSet _all = new ColumnSet(true);
QueryExpression _robQuery = new QueryExpression(EntityName);
_robQuery.ColumnSet = _all;
EntityCollection _allRec = prox.RetrieveMultiple(_robQuery);
return _allRec;
} //
C#: DATE-TIME FUNCTIONS
// DATE FUNCTIONS
// Get Current Year
protected int _currentYear()
{
DateTime dtn = DateTime.Now;
int ret= dtn.Year;
return ret;
}//**
// Get Current Month
protected int _currentMonth()
{
DateTime dtn = DateTime.Now;
int ret = dtn.Month;
return ret;
}//**
// Get current month in string
protected string _curMonthStr()
{
string thismonth = String.Format("{0:MMMM}", DateTime.Now).ToString();
return thismonth;
}
// Get Current Day
protected int _currentDay()
{
DateTime dtn = DateTime.Now;
int ret = dtn.Day;
return ret;
}//**
// Get Sundays for this month
protected int[] getSundays(int year, int month, DayOfWeek dayName)
{
int[] _sunday = new int[4];
CultureInfo ci = new CultureInfo("en-US");
int ai = 0;
for (int i = 1; i <= ci.Calendar.GetDaysInMonth(year, month); i++)
{
if (new DateTime(year, month, i).DayOfWeek == dayName)
{
//Response.Write(i.ToString());
_sunday[ai] = i; ai += 1;
}
}
return _sunday;
}//**
CRM 4: Create CRM Organization
public class CreateCrmOrg
{
public CreateCRM_Org()
{
static void Main()
{
DeploymentServiceClient service = Microsoft.Xrm.Sdk.Deployment.Proxy
.ProxyClientHelper.CreateClient(new Uri("http://srv-crm04/XRMDeployment/2011/Deployment.svc"));
Console.WriteLine(CreateOrganization(service
,new Organization
{
UniqueName = "testOrgProv1",
FriendlyName = "testOrgProv1",
SqlServerName = "SQL1-CRM04",
SrsUrl = "http://SQL1-CRM04/ReportServer",
BaseCurrencyCode = RegionInfo.CurrentRegion.ISOCurrencySymbol,
BaseCurrencyName = RegionInfo.CurrentRegion.CurrencyNativeName,
BaseCurrencySymbol = RegionInfo.CurrentRegion.CurrencySymbol,
State = Microsoft.Xrm.Sdk.Deployment.OrganizationState.Enabled
}));
}
Guid? CreateOrganization(IDeploymentService deploymentService,Organization org)
{
BeginCreateOrganizationRequest req = new BeginCreateOrganizationRequest
{
Organization = org
};
BeginCreateOrganizationResponse resp = deploymentService.Execute(req) as BeginCreateOrganizationResponse;
return resp != null ? (Guid?)resp.OperationId : null;
}
}
C#: Get num of days in month
DateTime.DaysInMonth(int year, int month);
//or
static int GetDaysInMonth(int year, int month)
{
DateTime dt1 = new DateTime(year, month, 1);
DateTime dt2 = dt1.AddMonths(1);
TimeSpan ts = dt2 - dt1;
return (int)ts.TotalDays;
}
C#: Get sundays in month
using System.Globalization;
protected void PrintSundays(int year, int month, DayOfWeek dayName)
{
CultureInfo ci = new CultureInfo("en-US");
for (int i = 1 ; i <= ci.Calendar.GetDaysInMonth (year, month); i++)
{
if (new DateTime (year, month, i).DayOfWeek == dayName)
Response.Write (i.ToString() + "<br/>");
}
}
C#: HttpWebRequest example with error handling using C#
using System;
using System.IO;
using System.Net;
using System.Text;
public class HttpWebRequestTool
{
public static void Main(String[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Missing argument. Need a URL and a filename");
}
else
{
StreamWriter sWriter = new StreamWriter(args[1]);
sWriter.Write(WRequest(args[0], "GET", ""));
sWriter.Close();
}
}
public static string WRequest(string URL, string method, string postData)
{
string responseData = "";
try
{
System.Net.HttpWebRequest hwrequest =
(System.Net.HttpWebRequest) System.Net.WebRequest.Create(URL);
hwrequest.Accept = "*/*";
hwrequest.AllowAutoRedirect = true;
hwrequest.UserAgent = "http_requester/0.1";
hwrequest.Timeout= 60000;
hwrequest.Method = method;
if (hwrequest.Method == "POST")
{
hwrequest.ContentType = "application/x-www-form-urlencoded";
// Use UTF8Encoding instead of ASCIIEncoding for XML requests:
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] postByteArray = encoding.GetBytes(postData);
hwrequest.ContentLength = postByteArray.Length;
System.IO.Stream postStream = hwrequest.GetRequestStream();
postStream.Write(postByteArray, 0, postByteArray.Length);
postStream.Close();
}
System.Net.HttpWebResponse hwresponse =
(System.Net.HttpWebResponse) hwrequest.GetResponse();
if (hwresponse.StatusCode == System.Net.HttpStatusCode.OK)
{
System.IO.Stream responseStream = hwresponse.GetResponseStream();
System.IO.StreamReader myStreamReader =
new System.IO.StreamReader(responseStream);
responseData = myStreamReader.ReadToEnd();
}
hwresponse.Close();
}
catch (Exception e)
{
responseData = "An error occurred: " + e.Message;
}
return responseData;
}
}
CRM 2011 JS: Reading XML file in CRM 2011
//Code Snippet
var nodePath = "//attributes/attribute";
var doc = new ActiveXObject("Microsoft.XMLDOM");
doc.preserveWhiteSpace = true;
doc.async = false;
doc.load(xmlPath);
params = new Array();
var nodelist;
nodelist = doc.selectNodes(nodePath);
for (var i = 0; i < nodelist.length; i++)
{
params[i] = nodelist(i).attributes[0].value;
}
CRM 2011 JS: Execute a Dialog using Jscript
//Code snippet
var dialogId = "a13ad982-d812-40a0-ab67-f314cdabbd2b"; // This must be your Dialog ID
var returnValue = showModalDialog("/" + Xrm.Page.context.getOrgUniqueName() +
"/cs/dialog/rundialog.aspx?DialogId=%7b" + dialogId +
"%7d&EntityName=account&ObjectId=" + Xrm.Page.data.entity.getId());
CRM 2011 JS: Clear Lookup using Jscript
//Code Snippet:
function SetLookupNull(lookupAttribute){
var lookupObject = Xrm.Page.getAttribute(lookupAttribute);
if (lookupObject != null)
{
Xrm.Page.getAttribute(lookupAttribute).setValue(null);
}
}