Sharepoint calculated field formula syntax - sharepoint

I have to programmatically set the formula for a calculated field in a list.
This field has to be the sum of two other fields. Can you provide me with the syntax for the formula that I have to insert in the code? I cannot find any example.
Can you provide me also with a reference for formula syntax because I have also to create another calculated field which is the concatenation of two string fields.
Thank you

Calculated Field Formulas reference:
For your task use simply:
=[Column1] + [Column2]
Create Calculated field with server code reference
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["Website_Name"])
{
SPList oList = oWebsite.Lists["MyList"];
SPFieldCollection collFields = oList.Fields;
string strNewFieldName = collFields.Add("MyNewColumn",
SPFieldType.Calculated, false);
SPFieldCalculated strNewField =
(SPFieldCalculated)collFields[strNewFieldName];
strNewField.Formula = "=[Column1]<[Column2]";
strNewField.Update();
}
Create Calculated field with CSOM code reference1, reference2. As you see below you need to remeember about escape special characters in xml.
string formula = "<Formula>=FirstName& \" \" &LastName& \" (id: \" &EmployeeID& \" \"</Formula>"
+ "<FieldRefs>"
+ "<FieldRef Name='FirstName' />"
+ "<FieldRef Name='LastName' />"
+ "<FieldRef Name='EmployeeID' />"
+ "</FieldRefs>";
string schemaCalculatedField = "<Field ID='<GUID>' Type='Calculated' Name='FullName' StaticName='FullName'
DisplayName='Full Name' ResultType='Text' Required='TRUE' ReadOnly='TRUE'>" + formula + "</Field>";
Field fullNameField = demoList.Fields.AddFieldAsXml(schemaCalculatedField, true, AddFieldOptions.AddFieldInternalNameHint);
clientContext.ExecuteQuery();
Working code for your implementation (comments):
string formula = "<Formula>=Nome1&" "&Cognome1&"(id:"&Campo1&")"</Formula>" + "<FieldRefs>" + "<FieldRef Name='Nome1'/>" + "<FieldRef Name='Cognome1'/>" + "<FieldRef Name='Campo1'/>" + "</FieldRefs>";
string schemaCalculatedField = "<Field ID='1F2ABCC0-D243-40F0-A18D-E0AEF7FE3EB6' Type='Calculated' Name='FullName' StaticName='FullName' DisplayName='Full Name' ResultType='Text' Required='TRUE' ReadOnly='TRUE'>" + formula + "</Field>";
Field fullNameField = context.Web.Lists.GetByTitle("PnP Custom List3").Fields.AddFieldAsXml(schemaCalculatedField, true, AddFieldOptions.AddFieldInternalNameHint);
context.ExecuteQuery();

Related

SharePoint 2010 CSOM get field values of a folder in document library

Using client object model, with below caml query able to fetch items with in a folder, but seeing a way to get "folder" field values where these items or documents is residing.
+ "<Query>"
+ " <Where>"
+ " <Eq><FieldRef Name='FSObjType' /><Value Type='int'>0</Value></Eq>"
+ " </Where>"
+ "</Query>"
My code for retrieving folder information...
string strFieldValue = string.Empty;
CamlQuery qryFolder = new CamlQuery();
qryFolder.ViewXml = #"<View Scope='RecursiveAll'>"
+ "<Query>"
+ " <Where>"
+ " <And>"
+ " <Eq><FieldRef Name='FSObjType' /><Value Type='int'>1</Value></Eq>"
+ " <Eq><FieldRef Name='FileRef' /><Value Type='Text'>"+folderName+"</Value></Eq>"
+ " </And>"
+ " </Where>"
+ "</Query>"
+ "<ViewFields>"
+ "<FieldRef Name='Title' /><FieldRef Name='FieldValue' /><FieldRef Name='FileRef' />"
+ "</ViewFields>"
+ "</View>";
qryFolder.FolderServerRelativeUrl = rootFolder;//+#"/"+folderName;
ListItemCollection itemColl = docs.GetItems(qryFolder);
context.Load(itemColl);
context.ExecuteQuery();
if (itemColl.Count == 1)
{
strFieldValue = itemColl[0]["FieldValue"].ToString();
}
return strFieldValue
I get a value here when used caml query with FSObjType is 1 which is for only folders... but unfortunately i get null when query with FSObjType is 0 which queries only files. My requirement is to get a value even when you are at the file level... Not sure if i'm going correct with the CAML query..
Thanks,
Jameel
add to you query this "<View Scope=\"RecursiveAll\"> ", just before the + "<Query>"
more details https://sharepoint.stackexchange.com/questions/29405/get-items-under-folder-caml

getting list of records in a subgrid using webapi or fetchxml

I want to get list of all records that are in a subgrid in a form
Is there a way to get it using some api call or using javascript?
The below code will fetch the rows if the subgrid is available in the form but
I don't want to have the subgrid in the form where I am going to fetch these records
var selectedRows = Xrm.Page.getControl("Contacts").getGrid().getSelectedRows();
var rows = Xrm.Page.getControl("Contacts").getGrid().getRows();
For example, assume that I have a medicalCase entity form, where I am having patients subgrid. I want to get the list of records in the patients subgrid using webapi in some other entity form
Yes, you can retrieve records using fetch.
Determine the fetchxml behind your view. Easiest way to do this is to pull up your view in Advanced Find and then use the Download Fetch Xml button.
Use a library like the xrmservicetoolkit to execute your fetch statement.
var fetchXml =
"<fetch mapping='logical'>" +
"<entity name='contact'>" +
"<attribute name='contactid' />" +
"<attribute name='firstname' />" +
"<attribute name='lastname' />" +
"<attribute name='accountrolecode' />" +
"<filter>" +
"<condition attribute='contactid' operator='eq' value='" + contactId + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";
var retrievedContacts = XrmServiceToolkit.Soap.Fetch(fetchXml);
console.log(retrievedContacts[0].attributes['lastname'].value)
See this page for more info.

Populating share-point look up columns via the web service

I am trying to populate a look-up column in a list, via the web service.
I am getting my data from an asp.net web form and using the web service method UpdateListItems and sending batch XML.
However, unless the user enters the exact data that the look up uses, the web service returns an error.
Is there anyway i can give the user of the web form, similar look-up functionality in order that the data passed will be identical?
i'm using share point 2007
The data source for the look-up column in share point is active directory.
_x0028_HR_x0029__x0020_Partner is the look up column, entering the users login name will look up their full name/ you can pick from a list.
Your help is much appreciated.
ClasService.Lists NewStarterList = new ClasService.Lists();
NewStarterList.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SPUserName"].ToString(),
ConfigurationManager.AppSettings["SPPassword"].ToString(), ConfigurationManager.AppSettings["SPDomain"].ToString());
NewStarterList.Url = ConfigurationManager.AppSettings["SPUrl"].ToString() + ConfigurationManager.AppSettings["SPServicePath"].ToString();
try
{
string strBatch = "<Method ID='1' Cmd='Update'>" +
"<Field Name='ID'>" + clasStarter.ClasID + "</Field>" +
"<Field Name='Title'>" + clasStarter.Name + "</Field>" +
"<Field Name='_x0028_HR_x0029__x0020_Job_x0020'>" + clasStarter.JobTitle + "</Field>" +
"<Field Name='Entity'>" + clasStarter.Entity + "</Field>" +
"<Field Name='Practice_x0020_Groups'>" + clasStarter.PracticeGroup + "</Field>" +
"<Field Name='Dept'>" + clasStarter.Department + "</Field>" +
"<Field Name='Physical_x0020_Desk_x0020_Locati'>" + clasStarter.Location + ", " + clasStarter.LocationInBuilding + ", " + clasStarter.Department + "</Field>" +
"<Field Name='_x0028_HR_x0029__x0020_Line_x002'>" + clasStarter.LineManager + "</Field>" +
"<Field Name='_x0028_HR_x0029__x0020_Buddy'>" + clasStarter.Buddy + "</Field>" +
"<Field Name='_x0028_HR_x0029__x0020_Partner'>" + clasStarter.Partner + "</Field>" +//is a look up
"</Method>";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ListVersion", "1");
elBatch.SetAttribute("ViewName", ConfigurationManager.AppSettings["SPViewID"].ToString());
elBatch.InnerXml = strBatch;
XmlNode ndReturn = NewStarterList.UpdateListItems(ConfigurationManager.AppSettings["SPListID"].ToString(), elBatch);
}
catch (Exception exp)
{
throw new Exception("NewStarterForm - Clas Update failed ", exp);
}
I haven't figured out a full solution my problem, but i have figured out part of it.
Share point look-up columns have two parts, an id and a value. If you don't know the value then you can just use -1;# as a substitute for the ID
an example of this is
"<Field Name='Partner'>-1;#" + partnerLogOnId + "</Field>"
I will probably use a list, that allows users of my form to select people, and will pass the partnerLogOnId to my web service method.

How to update a sharepoint list item via web services using a where clause?

I would like to update a list item using SharePoint and am stuggling to find 1 decent CAML example.
Here is what I want to do, in SQL my query would look something like this
update [table] set field='value' where fieldID = id;
so this would mean I have 1 item in a list I would like to update 1 field on given the ID of that listitem.
I've tried this, but it doesn't work:
batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +
"<Field Name='DeliveryStatus'>" + newStatus.ToString() + "</Field>" +
"<Where><Eq><FieldRef Name='ID' /><Value Type='Text'>" + id + "</Value></Eq></Where></Method>";
I'll add this answer for the community, although it might not answer all your questions.
batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +
"<Field Name='ID'>" + id + "</Field>" +
"<Field Name='DeliveryStatus'>" + newStatus.ToString() + "</Field></Method>";
It seems the first field you specify is the where clause.
I have no idea how you would do any advanced filtering with this (nots or exclusions or in clauses or ranges). But hope this basic info helps.
You don't need use the where clause to update a list item.
atchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +
"<Field Name='DeliveryStatus'>" + newStatus.ToString() + "</Field>" +
"<FieldRef Name='ID' /><Value Type='Text'>" + id + "</Value></Method>";
The only think you need do is to provide the ID like above.

Custom BusinessDataListWebPart Pagination (Next Button) - pageindex never changes

Pagination (Next button) doesn't work for custom BusinessDataListWebPart.
I am adding BusinessDataListWebPart using code. Everything works fine. I can see 20 data raw at the same time but when I click "Next Button", I can not see next 20-40 data. A postback occurs, but the pageindex never changes.
I am using following code to add BusinessDataListWebPart to the Sharepoint site.
BusinessDataListWebPart consumer = new BusinessDataListWebPart();
consumer.Title = title;
consumer.Application = instance.Name;
consumer.Entity = projEntity.Name;
consumer.XslLink = "/Style%20Library/XSL%20Style%20Sheets/" + xslFileName;
consumer.PageSize = 20;
OK..I found the answer.
For pagination I needed to add "ParameterBindings" to the business data list webpart.
My final code is, It works perfect.
BusinessDataListWebPart consumer = new BusinessDataListWebPart();
ServerContext serverContext = ServerContext.GetContext(site);
SqlSessionProvider.Instance().SetSharedResourceProviderToUse(serverContext);
LobSystemInstance instance = ApplicationRegistry.GetLobSystemInstanceByName(applicationName);
Entity projEntity = instance.GetEntities()[entityName];
consumer.Title = title;
consumer.Application = instance.Name;
consumer.Entity = projEntity.Name;
consumer.XslLink = "/Style%20Library/XSL%20Style%20Sheets/" + xslFileName;
consumer.PageSize = 20;
consumer.ParameterBindings = "<ParameterBinding Name=" + "\"dvt_firstrow\"" + " Location=" + "\"Postback;Connection\"" + "/>" +
" <ParameterBinding Name=" + "\"dvt_sortdir\"" + " Location=" + "\"Postback;Connection\"" + "/>" +
" <ParameterBinding Name=" + "\"dvt_sortfield\"" + " Location=" + "\"Postback;Connection\"" + "/>" +
" <ParameterBinding Name=" + "\"dvt_filterfields\"" + " Location=" + "\"Postback;Connection\"" + "/>" +
" <ParameterBinding Name=" + "\"dvt_partguid\"" + " Location=" + "\"Postback;Connection\"" + "/>";

Resources