Tuesday 25 October 2011

The code below can be used to correctly process the cookie string returned by a CRM data request.


public static Microsoft.Xrm.Sdk.Query.PagingInfo NextPageInfo(ref string cookie)
{
   if (String.IsNullOrWhiteSpace(cookie) == true)
      return new Microsoft.Xrm.Sdk.Query.PagingInfo();

   Microsoft.Xrm.Sdk.Query.PagingInfo rv = new Microsoft.Xrm.Sdk.Query.PagingInfo();
   rv.PagingCookie = cookie.Clone().ToString();
   // Now decode the
   System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
   xd.LoadXml(cookie);
   System.Xml.XmlNode p = xd.SelectSingleNode("cookie/@page");
   rv.PageNumber = int.Parse(p.Value) + 1;
   cookie = null;
   return rv;
}

Monday 24 October 2011

Get CRM 2011 Entity record count

using System;
using System.Collections.Generic;
namespace wa_Parracom_Upload {
   public static class CountRecords {
   public static int Count(Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy Org, string arg_entityName)
   {
      try {
         if (String.IsNullOrEmpty(arg_entityName)) return -1; // Argumet Usage
         string EntityName = arg_entityName.Trim().ToLowerInvariant();
         if (String.IsNullOrEmpty(EntityName)) return -1; // Invalid entity Name

         string PrimaryIdAttribute = "";
         try {
            Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest mrq = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest {
               LogicalName = EntityName,
               EntityFilters = Microsoft.Xrm.Sdk.Metadata.EntityFilters.Entity
            };
            Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse mrp =
               (Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse)Org.Execute(mrq);
            PrimaryIdAttribute = mrp.EntityMetadata.PrimaryIdAttribute;
         } catch (Exception) { return -1; /* Unknown Entity */ }

         System.Xml.XmlDocument fetch = new System.Xml.XmlDocument();
         {
            System.Xml.XmlNode MyXML_fetch = fetch.CreateNode(System.Xml.XmlNodeType.Element, "fetch", "");
            System.Xml.XmlNode MyXML_entity = fetch.CreateNode(System.Xml.XmlNodeType.Element, "entity", "");
            System.Xml.XmlNode MyXML_attribute = fetch.CreateNode(System.Xml.XmlNodeType.Element, "attribute", "");
            System.Xml.XmlAttribute MyXML_name = fetch.CreateAttribute("name", "");
            MyXML_name.Value = PrimaryIdAttribute;
            MyXML_attribute.Attributes.Append(MyXML_name);
            System.Xml.XmlAttribute MyXML_alias = fetch.CreateAttribute("alias", "");
            MyXML_alias.Value = "count";
            MyXML_attribute.Attributes.Append(MyXML_alias);
            System.Xml.XmlAttribute MyXML_aggregate = fetch.CreateAttribute("aggregate", "");
            MyXML_aggregate.Value = "count";
            MyXML_attribute.Attributes.Append(MyXML_aggregate);
            MyXML_entity.AppendChild(MyXML_attribute);
            System.Xml.XmlAttribute MyXML_name_1 = fetch.CreateAttribute("name", "");
            MyXML_name_1.Value = EntityName;
            MyXML_entity.Attributes.Append(MyXML_name_1);
            MyXML_fetch.AppendChild(MyXML_entity);
            System.Xml.XmlAttribute MyXML_mapping = fetch.CreateAttribute("mapping", "");
            MyXML_mapping.Value = "logical";
            MyXML_fetch.Attributes.Append(MyXML_mapping);
            System.Xml.XmlAttribute MyXML_aggregate_1 = fetch.CreateAttribute("aggregate", "");
            MyXML_aggregate_1.Value = "true";
            MyXML_fetch.Attributes.Append(MyXML_aggregate_1);

            fetch.AppendChild(MyXML_fetch);
         }
         Microsoft.Xrm.Sdk.Query.FetchExpression Fetch = new Microsoft.Xrm.Sdk.Query.FetchExpression(fetch.InnerXml);
         Microsoft.Xrm.Sdk.EntityCollection Ents = Org.RetrieveMultiple(Fetch);
         if (Ents.Entities != null && Ents.Entities.Count >= 1 && (Ents.Entities[0]).Contains("count")) {
            Microsoft.Xrm.Sdk.AliasedValue val = ((Ents.Entities[0])["count"]) as Microsoft.Xrm.Sdk.AliasedValue;
            if (val != null)
               return (int)val.Value;
            }
            return -1; // Unexpected result
         }
         catch (Exception) { return -1; /* Unknown Error */ }
      }
   }
}

Sunday 23 October 2011

Wednesday 19 October 2011

Set Proxy

http://technet.microsoft.com/en-us/library/cc731131(WS.10).aspx#BKMK_3
Netsh Commands for Windows Hypertext Transfer Protocol (WINHTTP)
netsh winhttp command
Where command is the command that you want to run, including all of the required parameters for the command.
For information on how to interpret netsh command syntax, see Formatting Legend.
This section contains the following commands:

Netsh winhttp commands

The following entries provide details for each command.

flush logbuffer

Flushes the internal buffers for the log files.

Syntax

flush loggbuffer

import proxy

Imports the proxy settings in the Internet Explorer Web browser's Internet Options. Importing settings from IE is the only available option

Syntax

import proxy source =ie

reset proxy

Resets the WinHTTP proxy setting to DIRECT.

Syntax

reset proxy

reset tracing

Resets the WinHTTP trace parameters to the default settings.

Syntax

reset tracing

Remarks

Following are the default WinHTTP trace parameters: