FetchXML doesn't return aggregate column when null values encountered - dynamics-crm-2011

I have a FetchXML query that reuturns two aggregate columns:
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='blocktrade'>
<attribute name='sourceref' alias='trade_count' aggregate='count'/>
<attribute name='allocationtradecount' alias='alloc' aggregate='sum'/>
<attribute name='organisation' alias='org' groupby='true'/>
</entity>
</fetch>
If I restrict the query to return objects that have values in allocationtradecount it works as expected. However, if some objects have null for the allocationtradecount, the column is not returned in the results!
i.e.
(int)((AliasedValue)e["alloc_count"]).Value;
fails. Is this 'expected'? How can I ensure 0 is used when null values are summed?

For this you need to first check whether that record contains any value.
Like shown in following code...
Consider, you want to assign value to int sum.
int sum = e.Attributes.Contains("alloc") ? (int)((AliasedValue)e["alloc"]).Value : 0;
Above code first check whether record contains any value if yes it will return sum else it will return 0.
Hope this helps.

Related

On fetch return error 0x80040203

I'm trying to select the first 10 records "new_region" in MS Dynamical CRM
"<fetch mapping= ""logical"" count=""10"">"+
"<entity name='new_region'>"+
"<attribute name='new_regionid'/>"+
"</entity>"+
"</fetch>"+
Error:
<code>0x80040203</code>
<description>entity</description>
<type>Platform</type>
Please, help me!
Try this
string getRecords = #"<fetch mapping= 'logical' distinct ='true' count='10'>
<entity name='new_region'>
<attribute name='new_regionid'/>
<order attribute = 'new_name' descending = 'false'/>
</entity>
</fetch>";

CAML person or Group field with Multiple values

I have a Field with Name TargetedPeople in a Sharepoint list. This is a Person or User group which can have multiple values.
The CAML I used for Querying is
siteDataQuery.Query = #"<Where><Includes><FieldRef Name='TargetedPeople' LookupId='TRUE'/><Value Type='Integer'>" + webInContext.CurrentUser.ID + "</Value></Includes></Where>";
This works fine if we have set the Allow Multiple Selections for "No" in the field. But this seems like not working for if it set to "Yes".
Please share me how to query a multiple field.
After some changes I was able to figure out this. If the field has multiple values we should define the value Type as LookupMulti. Following is the working code sample
siteDataQuery.Query = #"<Where><Contains><FieldRef Name='TargetedPeople' LookupId='TRUE'/><Value Type='LookupMulti'>" + webInContext.CurrentUser.ID + "</Value></Contains></Where>";
Also works and looks like simpler (tested on SP Online):
<Contains>
<FieldRef Name='TargetedPeople' />
<Value Type="Integer">
<UserID Type="Integer" />
</Value>
</Contains>

Element <Batch> of parameter updates is missing or invalid exception occurs while deleting sharepoint list record

I'm getting
Element <batch>
of parameter updates is missing or invalid
The error occurs while deleting sharepoint list record. The sharepoint list does not have any required column.
Following is the code:
string batchCommand = "<Batch OnError='Continue'><Method ID='1' Cmd='Delete'>";
batchCommand += string.Concat("<Field Name='CrfiId'>", rowId);
batchCommand += "</Field></Method></Batch>";
XmlElement batchElement = xmlDoc.CreateElement("batchElement");
batchElement.InnerXml = batchCommand;
lock (_lock)
{
xmlReturn = client.UpdateListItems(listName, batchElement);
}
Any idea why is this error occurring?
Use ID instead of CrfiID.. You need to filter by the built-in id field, it won't work with user created fields.
You need to specify your XML as follows;
<Batch OnError="Continue" ListVersion="1">
<Method ID="1" Cmd="Delete">
<Field Name="ID">itemId</Field>
</Method>
</Batch>
Where, for example;
string itemId = 5;

Querying Sharepoint - Value does not fall within the expected range

I'm attempting to retrieve the value of a listitem but keep getting a ArgumentException - Value does not fall within the expected range.
My code is as follows:
if (resultList.Count > 0)
{
SPListItem result = resultList[0];
if (result[Column] != null)
{
return result[Column].ToString();
}
}
In the immediate window I can verify the column does exist and the value can be found in the object tree structure.
result.Fields.GetField(Column).Id
returns a Guid but using it to retrieve the value of the Field results in another ArgumentException:
result[result.Fields.GetField(Column).Id]
This could happen if you get your list item collection from view (list.GetItems(view)) or from query with ViewFields property set, in this case only the fields included in ViewFields are returned.
You need to use InternalName of the field to get its value from SPListItem
result[result.Fields.GetField(Column).InternalName]

How can I write a SPQuery to filter items based on a LinkFieldValue?

I need to select a single value from a SharePoint list based on a field value. The type of the field is LinkFieldValue. How should I write the CAML query?
When I select the items with an empty query, I receive all the items in the list as expected.
When I add constraints to the query, it returns an empty result. I have tried constructing the query as follows:
string.Format("<Where><Eq><FieldRef Name=\"PollInstancePoll\" /><Value "
+"Type=\"Text\">{0}</Value></Eq></Where>",
new LinkFieldValue { NavigateUrl = "/az/Lists/Polls/DispForm.aspx?ID=1",
Text = "example poll" });
which results in the following query text:
<Where><Eq><FieldRef Name="PollInstancePoll" />
<Value Type="Text">example poll</Value>
</Eq></Where>
I have solved my problem with the following query:
new SPQuery
{
Query =
CAML.Where(
CAML.And(
CAML.Contains(
CAML.FieldRef("PollInstancePoll"),
CAML.Value(pollPath)),
CAML.Contains(
CAML.FieldRef("PollInstancePage"),
CAML.Value(pagePath))))
};
Essentially I am checking only the URL part of the Link field, and providing the value for comparison as Type="Text". It is important to remember that SharePoint stores the values in database always as server-relative URLs.

Resources