set lookup field & managed meta data field using jsom in sharepoint - sharepoint

Setting lookup field & managed meta data field value using jsom. Through jsom I will need to set the value into the list .
Setting the lookup and managed metadata columns through code

Try and modify the below sample code:
var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);
var list = clientContext.get_web().get_lists().getByTitle('TestList');
var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);
var singleLookupColumn = new SP.FieldLookupValue();
singleLookupColumn.set_lookupId(2);
listItem.set_item('CustomLookup', singleLookupColumn);
var field = list.get_fields().getByInternalNameOrTitle("TestTaxonomy");
var taxField = clientContext.castTo(field, SP.Taxonomy.TaxonomyField);
var taxonomyCol = new SP.Taxonomy.TaxonomyFieldValue();
taxonomyCol.set_label("Test");
taxonomyCol.set_termGuid("23d03b66-5be6-512b-9fe3-ff13b9b4757c");
taxonomyCol.set_wssId(-1);
taxField.setFieldValueByValue(listItem, taxonomyCol);
listItem.update();
clientContext.load(listItem);
clientContext.executeQueryAsync(function(){
console.log("success");
},function(){
console.log("error");
});

Related

How to add extra filter and columns into existing saved searches while loading in Netsuite 2.0

I have the following SuiteScript 2.0 code in a UserEvent where I would like to add an additional filter and columns to the loaded saved search.
A filter is working properly but how to get column value from Array which is added as an extra column in Saved search.
var filters = [];
filters.push(
['memo', 'is', 'Updated']
);
var filters = [];
filters.push(
['memo', 'is', 'Updated']
);
var columnsCust = [];
columnsCust.push(search.createColumn({
name: 'trandate'
}));
var mySearch = search.load({
id: 'customsearch_so_savedsearch'
});
//Add filters
mySearch.filterExpression = filters;
var filtersResult = mySearch.filterExpression;
//Add columns
mySearch.column = columnsCust;
var columnResult = mySearch.column;
var searchResult = mySearch.run().getRange({start: 0,end: 10});
for (var i = 0; i < searchResult.length; i++)
{
var date = searchResult[i].getValue({name: 'trandate'});
log.debug('date::' + date); //date::null
//it gives transactionnumber value Because this column exist in Saved
//search.
var transactionnumber = searchResult[i].getValue({name: 'transactionnumber'});
log.debug('transactionnumber::' + transactionnumber); //transactionnumber::112513
}
To add extra columns, filters or filterExpressions in a search object, firsrt you need to fetch the object from search-object and then update it.
For Search Columns
var searchColumns = mySearch.columns;
searchColumns.push(AdditionalColumns);
mySearch.columns = searchColumns;
For Search Filters
var searchFilters = mySearch.filters;
searchFilters.push(additionalFilters);
mySearch.filters = searchFilters;
For FilterExpressions
var searchFilterExpression = mySearch.filterExpression;
// push operator if searchObject contains filters
if (searchFilterExpression.length > 0) {
searchFilterExpression.push('and');
}
searchFilterExpression.push(additionalFilterExpression);
mySearch.filterExpression = searchFilterExpression;
Note: Check this out for further reading.

How do I update NetSuite Department isInactive via WSDL?

I'm trying to update NetSuite Department via WSDL but I'm having an issue updating isInactive. Below is my code in C#:
var record = new com.netsuite.webservices.Department
{
internalId = dp.InternalId,
isInactive = dp.InActive
};
then called
var result = ServiceClient.update(record);
The Department's DEPARTMENT IS INACTIVE on NetSuite doesn't check whether I set it to true or false. What am I doing wrong here?
You forgot to set isInactiveSpecified
Try this:
var record = new com.netsuite.webservices.Department
{
internalId = dp.InternalId,
isInactive = dp.InActive,
isInactiveSpecified = true
};
You need to first .get() the record, set some properties, then .update() the record. Here's what works for me:
var ns = new NetSuite.NetSuiteService();
// passport info skipped
var departmentRef = new RecordRef
{
internalId = "1",
type = RecordType.department,
typeSpecified = true
};
var response = ns.get(departmentRef);
var department = response.record as Department;
department.isInactive = true;
ns.update(department);

SharePoint JSOM Parsing values in a hyperlink field

I have a value that looks like "mailto:a#b.com, mailto:a#b.com'. This is basically a hyperlink field and I want to parse this properly using SharePoint JSOM. I tried SP.FieldUrlValue, but it does not seem to have a method that lets you parse.
You can use the .get_url() function on the actual item value to get the hyperlink URL, or the .get_description() function to get the hyperlink's display text.
var linkField = "internalColumnName";
var listName = "List Title";
var clientContext = new SP.ClientContext();
var list = clientContext.get_web().get_lists().getByTitle(listName);
var camlQuery = new SP.CamlQuery();
var items = list.getItems(camlQuery);
clientContext.load(items);
clientContext.executeQueryAsync(Function.createDelegate(this,function(){
var itemEnumerator = items.getEnumerator();
while(itemEnumerator.moveNext()){
var item = itemEnumerator.get_current();
var url = item.get_item(linkField).get_url(); // <-- Get URL
var text = item.get_item(linkField).get_description(); // <-- Get Text
alert(url + ", " + text);
}
}),Function.createDelegate(this,function(sender, args){alert(args.get_message());}));

How Do I get a page's URL using JSOM

I am using SharePoint 2013 workflow.
I am in the Initiation form when my my users clock the Start button to start the workflow.
I am using JSOM to start the workflow but since I am on the Initiation form, I don't know the URL of the page. I do know the list (pages) and the the list id (2).
Can someone help me retrieve the list id's url using JSOM?
Thanks
Tom
How to get Page Url in Initiation Form page:
var listId = getParameterByName('List');
var itemId = getParameterByName('ID');
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getById(listId);
var listItem = list.getItemById(itemId);
ctx.load(listItem);
ctx.executeQueryAsync(
function () {
var itemUrl = listItem.get_item('FileRef');
console.log(itemUrl);
},
function (sender, args) {
console.log(args.get_message());
}
);
,where
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
is intended for retrieving parameter from query string
Source

List 'created by' in column

I know there is surveylist.get_description and surveylist.get_itemCount. I would like to know if there is a way to get created by fit in a column?
From syntax i guess you are using Javascriot CSOM.
Try this:
surveylist.get_author
Don't forget that you need to explicitly specify which properties you want to retrieve before you can get their values.
Unfortunately List Author property is not exposed via SharePoint CSOM.
How to retrieve List Author property via CSOM?
The idea is to retrieve SPList.SchemaXml property and extract Author property
function getListAuthor(listTitle,OnSuccess,OnError) {
var context = SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
context.load(web);
context.load(list,'SchemaXml');
context.executeQueryAsync(function(sender,args){
var schemaXml = $.parseXML(list.get_schemaXml());
var authorId = parseInt($(schemaXml).find('List').attr('Author')); //SchemaXml contains Author ID only
var listAuthor = web.getUserById(authorId);
context.load(listAuthor);
context.executeQueryAsync(OnSuccess(listAuthor),OnError);
},OnError);
}
//Usage
getListAuthor('Discussions List',
function(author){
console.log('List created by: ' + author.get_loginName())
},
function(sender,args){
console.log('Error occured:' + args.get_message());
}
);

Resources