Wednesday 29 February 2012

Xrm.Page.ui


file deletion error

When trying to delete the file getting following message
"The action can't be completed because the file is open in COM Surrogate",

Kill the dllhost process in task manager, and then delete the file.

For reason why the message is shown see following url

http://blogs.msdn.com/b/oldnewthing/archive/2009/02/12/9413816.aspx

Thursday 23 February 2012

Creating dropdown menu in crm 2011 ribbon bar



<RibbonDiffXml>
        <CustomActions>
          <CustomAction Id="Mscrm.Homepage.customentity.MainTab.Collaborate.Buttons.Form.Correspondance" Sequence="40" Location="Mscrm.Form.new_supplementarydata.MainTab.Collaborate.Controls._children">
            <CommandUIDefinition>
              <FlyoutAnchor
                Command="Mscrm.Enabled"
                Id="new.Homepage.customentity.Collaborate.Correspondance"
               
                Image16by16="$webresource:new_accept1616"
                Image32by32="$webresource:new_accept3232"
                LabelText="$LocLabels:new.customentity.Collaborate.BusinessX.LabelText"
                PopulateDynamically="true"
                PopulateQueryCommand="new.Homepage.customentity.Form.PopulateMenu"
                Sequence="14"
                TemplateAlias="o1"
               
                 PopulateOnlyOnce="true"
               
               
                Alt="$LocLabels:new.customentity.Collaborate.BusinessX.LabelText"
                ToolTipDescription="$LocLabels:new.customentity.Collaborate.BusinessX.ToolTipDescription"
                ToolTipTitle="$LocLabels:new.customentity.Collaborate.BusinessX.LabelText"
              />        
            </CommandUIDefinition>
          </CustomAction>
        </CustomActions>
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"/>
        </Templates>
        <CommandDefinitions>
          <CommandDefinition Id="new.Homepage.customentity.Form.PopulateMenu">
            <EnableRules>
              <EnableRule Id="Mscrm.Enabled "/>
            </EnableRules>
            <DisplayRules>
              <DisplayRule Id="publishername.contact.WebClient.DisplayRule"/>
            </DisplayRules>
            <Actions>
              <JavaScriptFunction Library="$webresource:new_API.customentity" FunctionName="PopulateBusinessXMenu" >
                <CrmParameter Value="CommandProperties"/>
              </JavaScriptFunction>
            </Actions>
          </CommandDefinition>
          <CommandDefinition Id="new.customentity.Command.Menu">
            <EnableRules />
            <DisplayRules />
            <Actions>
              <JavaScriptFunction Library="$webresource:new_API.customentity" FunctionName="BusinessXSelectedTemplate">
                <CrmParameter Value="CommandProperties"/>
              </JavaScriptFunction>
            </Actions>
          </CommandDefinition>
        </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules/>
          <DisplayRules>
            <DisplayRule Id="publishername.contact.WebClient.DisplayRule">
              <CrmClientTypeRule Type="Web"/>
            </DisplayRule>
          </DisplayRules>
          <EnableRules/>
        </RuleDefinitions>
        <LocLabels>
          <LocLabel Id="new.customentity.Collaborate.BusinessX.LabelText">
            <Titles>
              <Title languagecode="1033" description="BusinessX"/>
            </Titles>
          </LocLabel>
          <LocLabel Id="new.customentity.Collaborate.BusinessX.ToolTipDescription">
            <Titles>
              <Title languagecode="1033" description="Click to select the BusinessX template"/>
            </Titles>
          </LocLabel>
        </LocLabels>
      </RibbonDiffXml>

------------------------------
and javascript is

function PopulateCorrespondenceMenu (CommandProperties) {

        var menuXml = '<Menu Id="new.Homepage.customentity.Collaborate.BusinessX.Menu"><MenuSection Id="new.Homepage.customentity.Collaborate.BusinessX.Menu.MenuSelection" Sequence="10"><Controls Id="new.Homepage.customentity.Collaborate.BusinessX.Menu.MenuSelection.Controls">';
        menuXml = menuXml + '<Button Id="new.Homepage.customentity.Collaborate.BusinessX.Menu.MenuSelection.Controls.Button1" Command="new.customentity.Command.Menu" LabelText="Button1" ToolTipTitle="Template1" ToolTipDescription="Template1 desc"  TemplateAlias="Template1 alias"  Sequence="20" Image16by16="$webresource:new_accept1616" Image32by32="$webresource:new_accept3232" />';
        menuXml = menuXml + '<Button Id="new.Homepage.customentity.Collaborate.BusinessX.Menu.MenuSelection.Controls.Button2" Command="new.customentity.Command.Menu" LabelText="Button2" ToolTipTitle="Template2" ToolTipDescription="Template2 desc"  TemplateAlias="Template2 alias"  Sequence="20" Image16by16="$webresource:new_accept1616" Image32by32="$webresource:new_accept3232" />';
        menuXml = menuXml + '</Controls> </MenuSection> </Menu>';

        CommandProperties.PopulationXML = menuXml;

    }


function    CorrespondenceSelectedTemplate (CommandProperties) {
        var controlId = CommandProperties.SourceControlId;
        alert("correspondence selected " + controlId);
    }



FlyoutAnchor PopulateDynamically not working/error


FlyoutAnchor for dynamically generating menu is not working, when trying to set

CommandProperties.PopulationXML in javascript.

Resoultion:
instead of '<Menu Id=\"Sample.DynamicMenu\">', use '<Menu Id="Sample.DynamicMenu">'
the difference is of not using escape character for "

Wednesday 22 February 2012

creating ribbon button

Create a temp solution and add the new/existing entity where we like to create the custom ribbon button,
then export the solution as unmanaged and then unzip the file and open the cusomization.xml file
and update the  RibbonDiffXml node,
If using Visual Studio, go to XML option and select "Schemas..." then add the schema file "customizationssolution.xsd" available in the crm2001 sdk at "sdk\schemas", doing this validates the xml file



<RibbonDiffXml>
  <CustomActions>
    <CustomAction Id="publishername.Homepage.entityname.CustomTab.Groups" Sequence="40" Location="Mscrm.SubGrid.entityname.MainTab.Workflow.Controls._children">
      <CommandUIDefinition>
        <Button Id="publishername.Homepage.entityname.CustomTab.FirstGroup.FirstButton" Sequence="11" TemplateAlias="o1" Image32by32="$webresource:publishername_clientemail16.gif" Image16by16="$webresource:publishername_clientemail32.gif" Alt="$LocLabels:publishername.entityname.CustomTab.FirstGroup.FirstButton.LabelText" LabelText="$LocLabels:publishername.entityname.CustomTab.FirstGroup.FirstButton.LabelText" Command="publishername.Homepage.entityname.FirstButton" ToolTipDescription="$LocLabels:publishername.entityname.CustomTab.FirstGroup.FirstButton.ToolTipDescription" ToolTipTitle="$LocLabels:publishername.entityname.CustomTab.FirstGroup.FirstButton.LabelText"/>
      </CommandUIDefinition>
    </CustomAction>
  </CustomActions>
  <Templates>
    <RibbonTemplates Id="Mscrm.Templates"/>
  </Templates>
  <CommandDefinitions>
    <CommandDefinition Id="publishername.Homepage.entityname.FirstButton">
      <EnableRules>
        <EnableRule Id="Mscrm.Enabled "/>
      </EnableRules>
      <DisplayRules>
        <DisplayRule Id="publishername.contact.WebClient.DisplayRule"/>
      </DisplayRules>
      <Actions>
        <JavaScriptFunction Library="$webresource:new_javascript" FunctionName="SelectedGridIds">
          <CrmParameter Value="SelectedControlSelectedItemIds"></CrmParameter>
        </JavaScriptFunction>
      </Actions>
    </CommandDefinition>
  </CommandDefinitions>
  <RuleDefinitions>
    <TabDisplayRules/>
    <DisplayRules>
      <DisplayRule Id="publishername.contact.WebClient.DisplayRule">
        <CrmClientTypeRule Type="Web"/>
      </DisplayRule>
    </DisplayRules>
    <EnableRules/>
  </RuleDefinitions>
  <LocLabels>
    <LocLabel Id="publishername.entityname.CustomTab.FirstGroup.FirstButton.LabelText">
      <Titles>
        <Title languagecode="1033" description="Do Action"/>
      </Titles>
    </LocLabel>
    <LocLabel Id="publishername.entityname.CustomTab.FirstGroup.FirstButton.ToolTipDescription">
      <Titles>
        <Title languagecode="1033" description="Perform action on selected items in subgrid"/>
      </Titles>
    </LocLabel>
  </LocLabels>
</RibbonDiffXml>

--------------------------

Enable rule for existing record

 <EnableRules>
            <EnableRule Id="prefix.entity.Command.Existing">
              <FormStateRule State="Existing"/>
            </EnableRule>
          </EnableRules>

Dynamically generating drop down menu in ribbon bar

Monday 20 February 2012

CRM 2011 and Sharepont integration

crm 2011 javascript/jscript

var objectlookup = Xrm.Page.getAttribute("new_lookup").getValue()
if(objectlookup){
objectlookup[0].id//entity id
objectlookup[0].type//entity logical name

}
escape(string) //to encode string/url
unescape(string) //to decode string/url

//working with two values optionset
Xrm.Page.getAttribute("new_enable").getValue();
Xrm.Page.getAttribute("new_enable").setValue(false);

Xrm.Page.ui.controls.get(" new_enable ").setDisabled(true);

Xrm.Page.ui.controls.get(" new_enable ").setVisible(false);


To open a browser window in maximized mode

var features = 'left=0,top=0,width=' + (screen.availWidth - 10) + ',height=' + screen.availHeight + ',toolbar=0,resizable=1';
var returnValue = window.open(url, 'mylink', features);


To resize the current browser

 parent.top.window.moveTo(0, 0);
 parent.top.window.resizeTo(screen.availWidth, screen.availHeight);


Xrm.Page.getAttribute("attribute_name").getValue();

Xrm.Page.getAttribute("attribute_name").setValue();

Xrm.Page.data.entity.save( null | "saveandclose" |"saveandnew" )




Cancel save event
event.returnValue = false;

To always save the value of attribute

Xrm.Page.getAttribute("new_attribute").setSubmitMode("always");


Xrm.Page.ui.getFormType();
1 –  Form context is Create
2 –  Form context is Update


LoadForm: function () {
        var Form1Id = "guid of create form";
        var Form2Id = "guid of update form";
        var currentFormId = Xrm.Page.ui.formSelector.getCurrentItem().getId();

        var formType = Xrm.Page.ui.getFormType();

        if (formType == 1 && Form1Id !=  currentFormId  ) {

            Xrm.Page.ui.formSelector.items.get(Form1Id).navigate();

        }
        else if (formType == 2 && Form2Id !=  currentFormId  ) {

            Xrm.Page.ui.formSelector.items.get(Form2Id).navigate();

        }
    }

////////////
new Date(year, month, day, hours, minutes, seconds, milliseconds)

new Date(targetdateStr.substring(0, 10).replace(/-/g, '/'));

new Date(todayDateTime.getYear(), todayDateTime.getMonth(), 

todayDateTime.getDay(), 0, 0, 0, 0);
////////////////////
if( date1 < date2){ // do something}

try javascript  at http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_date_weekday

http://gtcrm.wordpress.com/2011/03/16/java-script-referenceupdated-2/

enumeration in javascript

custom.namespace.Enum = {
  Option1: "option1 value",
 Option2: "option2 value"
}

to use enum
if(selectedText == custom.namespace.Enum.Option1){
...
}



LoadForm: function () {
        var formType = Xrm.Page.ui.getFormType();

        if (formType == EntityName.FormType.Create) {

            var attributeLookup = Xrm.Page.getAttribute("new_attribute1").getValue();

            var service = new crmSOAPService(orgUniqueName, server);

            var contract = service.Retrieve(attributeLookup [0].entityType, attributeLookup [0].id, ['customerid']);

            if (contract.attributes.customerid.type == "account") {
                var customer = contract.attributes.customerid;
                Xrm.Page.getAttribute("new_attribute2").setValue([{ id: customer.value, name: customer.name, entityType: customer.type}]);
                Xrm.Page.getAttribute("new_attribute2").setSubmitMode("always");
            }
        }
    }


ReLoadParentForm: function () {

        window.parent.opener.parent.location.reload();
    }

Identity

 Environment.UserName


To get the current user identity use the above line.

Associate and Dissociate Message event

To use this event register it on primary and secondary entity "none" and then use code to figure out which entity is it mapped firing on


EntityReference target = (EntityReference)parameters["Target"];
Relationship relationship = (Relationship)parameters["Relationship"];
EntityReferenceCollection relatedEntities = (EntityReferenceCollection)parameters["RelatedEntities"];

where target is the source entity to which entities are getting associated with. and related entities is the collection of entities which are getting associated with target entity.

in order to disallow any selection, register the plugin on "PreInsideTransaction"


http://www.avanadeblog.com/xrm/2010/05/i-find-your-lack-of-events-disturbing.html

Thursday 16 February 2012

Working with solutions

On Org1
I have core solution as unmanaged

On Org2
core solution as managed
custom solution as unmanaged

On Org3
core solution as managed
custom  solution as managed
custom2 as unmanaged

Stagging
core solution as managed
custom solution as managed
custom2 solution as managed

if there is an issue in custom solutions, then I work on Org2 and mitigate the issue and export it as managed and on Org3 and stagging import it as managed with overwrite existing customizations and selecting the checkbox of active process and messages, otherwise it will give a good pain to enable all the steps.

Any step which is changed which was part of managed solution will be overwritten

if when trying to import solution gives error that some pluin is missing then register that pluin using pluin registeration tool and then import managed solution, and then select appropirate options.

Monday 6 February 2012

custom subgrid



or

LoadSubGrid : function () {

        var entityId= Xrm.Page.data.entity.getId();
        var  new_lookupattribute = Xrm.Page.getAttribute("new_lookupattribute").getValue();
        var  new_lookupattributeId = null;
        if ( new_lookupattribute ) {
              new_lookupattributeId =  new_lookupattribute [0].id;
        }


        var fetchXml = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\">";
        fetchXml = fetchXml + "<entity name=\"new_entity\">";
        fetchXml = fetchXml + "<attribute name=\"new_attribute1\" />";
        fetchXml = fetchXml + "<attribute name=\" new_attribute2\" />";
        fetchXml = fetchXml + "<attribute name=\" new_attribute3\" />";
        fetchXml = fetchXml + "<link-entity name=\"new_entity2\" from=\"new_entityid\" to=\"new_entity2id\" visible=\"false\" link-type=\"outer\" alias=\"a_1ee52836f351e11198888asasd450361\">";
        fetchXml = fetchXml + "<attribute name=\"new_entity2attribute\" />";
        fetchXml = fetchXml + "</link-entity>";
        fetchXml = fetchXml + "<filter type=\"and\">";
        fetchXml = fetchXml + "<condition attribute=\"new_attriubteid\" operator=\"eq\" uiname=\"ds32\" uitype=\"new_entity3\" value=\"" +  entityId + "\" />";
        if ( new_lookupattributeId) {
            fetchXml = fetchXml + "<condition attribute=\"new_entity4id\" operator=\"eq\" uiname=\"entity4\" uitype=\"new_Entity4\" value=\"" +  new_lookupattributeId + "\" />";
        }


        fetchXml = fetchXml + "</filter>";
        fetchXml = fetchXml + "</entity>";
        fetchXml = fetchXml + "</fetch>";


        PopulateGridFetchXML("LoadSubGrid()", 'SubGridName', fetchXml);
    }




PopulateGridFetchXML: function (sourceFunctionName, subGrid, fetchXML) {
        var updateXml = function (sourceFunctionName, subGrid, field, fetchXML) {
            try {
                if (Xrm.Page.getControl(subGrid)._control.get_innerControl() == null) {
                    setTimeout(sourceFunctionName, 1000);
                    return;
                }
                Xrm.Page.getControl(subGrid)._control.get_innerControl().setParameter(field, fetchXML);
                // Refresh the grid
                Xrm.Page.getControl(subGrid).refresh();
            } catch (e) { }
        };
        // Update the fetchXml
        updateXml(sourceFunctionName, subGrid, "fetchXml", fetchXML);
    }


set state not working

set state using jscript

Sunday 5 February 2012

javascript is not working "access denied"


I have done bit of customisation on entity record by adding ribbon button and on pressing that button I call code from server, its works fine form that machine having url of "http://devserver/orgname/main.aspx however when I am trying to access my machine from a different machine with fully qualified name something like http://devserver.active.local/orgname/main.aspx my javascript is not working, and when I tried to close the page I was prompted with error screen and when I debuged javascript following is the line which was showing an error of "access denied" when trying to open connection,

var oXmlHttp = this.CreateXmlHttp();
oXmlHttp.open("POST", this.server + "/mscrmservices/2007/crmservice.asmx", (fUserCallback != null));

Here the this.server value is taken from Xrm.Page.context.getServerUrl() and is "devserver", and I am trying to access it from "devserver.active.local",

Reason:
It's a security thing to prevent people from using AJAX between websites and servers.

Basically, whenever you write a URL in AJAX, it has to be the same URL as the website you are running the script on. Even the www. has to be the same.


http://www.webdeveloper.com/forum/showthread.php?t=147342


Enable the option in the browser to "Access data sources across domain"

http://social.microsoft.com/Forums/en-US/04d357e2-847d-45af-bc34-f4f265fe1bbb/i-am-getting-access-denied-error-when-using-xmlhttprequest-in-javascript-to-fetch-data-from-database
 

Saturday 4 February 2012

soap service javascript

http://community.dynamics.com/product/crm/crmtechnical/b/xrmavanade/archive/2010/07/18/updated-crmservice-js.aspx


            var service = new crmSOAPService(orgUniqueName, server);
                        var columnset = ['attributename'];
                        var setpinstance = service.Retrieve("entityname", selectedIds[0], columnset);
                        setpinstance.attributes["attibutename"];

setting lookup/entityreference field
var lookupvalue = [{ id: selectedIds[0], name: "name value", entityType: "entityname"}] ;

Xrm.Page.getAttribute("attriubtename").setSubmitMode("always");
Xrm.Page.getAttribute(" attriubtename ").steptocloseLookup.setValue( lookupvalue  );


---------------
use advance find to generate fetch xml


 
      var orgUniqueName = context.getOrgUniqueName(); 
      var server = Xrm.Page.context.getServerUrl().replace("/" + orgUniqueName, "");


      var service = new crmSOAPService(orgUniqueName, server);
   
      var fetchResult = service.Fetch(fetchXml);
      var  longestInstance  = null;

        if (fetchResult.length > 0) {
            var prevDiff = 0;

            for (instance in fetchResult) {

                var startDateString = fetchResult[instance].attributes.new_startdate.value; //"2012-01-30T00:00:00-00:00"
                var endDateString = fetchResult[instance].attributes.new_enddate.value;

                var startDate = new Date(startDateString.substring(0, 10).replace(/-/g, '/')); //Mon Jan 30 00:00:00 UTC 2012 Object
                var endDate = new Date(endDateString.substring(0, 10).replace(/-/g, '/'));

                var diff = Math.abs(endDate - startDate);

                //alert("GetLongestStep:diff" + diff);

                if (prevDiff < diff) {
                    prevDiff = diff;
                    longestInstance = fetchResult[instance];
                }
            }
        }

------------

Thursday 2 February 2012

Sending Email from xRM/CRM plugin


// Here toList contains both system user and contact references

public static void SendEmail(IOrganizationService crmService, EntityReference from, List<EntityReference> toList, string subject, string body)
        {
            Entity fromParty = new Entity("activityparty");
            fromParty["partyid"] = from;

            Entity[] toPartyArray = new Entity[toList.Count];

            for (int i = 0; i < toList.Count; i++)
            {
                Entity toParty = new Entity("activityparty");
                toParty["partyid"] = toList[i];

                toPartyArray[i] = toParty;
            }

            Entity email = new Entity("email");
            email["from"] = new Entity[] { fromParty };
            email["to"] = toPartyArray;

            email["subject"] = subject;
            email["description"] = body;

            Guid emailId = crmService.Create(email);

            OrganizationRequest emailRequest = new OrganizationRequest("SendEmail");
            emailRequest.Parameters.Add("EmailId", emailId);
            emailRequest.Parameters.Add("IssueSend", true);
            emailRequest.Parameters.Add("TrackingToken", string.Empty);
            crmService.Execute(emailRequest);
        }


http://rajeevpentyala.wordpress.com/2011/08/03/sending-an-email-using-crm-2011-plug-in/

http://blogs.infinite-x.net/2006/12/04/sendemailrequesttrackingtoken-field-documentation-error/


When sending an email on change of ownership of record, I was getting error "some error occurred", to resolved it set the caller id "on behalf of" as below


            EntityReference callingUser = new EntityReference
            {
                LogicalName = "systemuser",
                Id = localContext.PluginExecutionContext.InitiatingUserId
            };

            OrganizationServiceProxy serviceProxy = (OrganizationServiceProxy)crmService;
            serviceProxy.CallerId = callingUser.Id;