Dynamic Object Reference data.d.results.[fieldobjectname].[fieldname] in JavaScript - sharepoint

BACKGROUND:
Using SharePoint 2013, calling a REST Ajax call in javascript. When successful, this returns results in a data set, which I put in a variable called data.
My OData syntax uses $expand on a SharePoint Person {the datatype} field, such that I can return the person's name, as well as their ID.
So, in the call.done function(data, status, jqXHR) , if the field being "expanded" is called "Employee", then the data results can be accessed by using a for loop and step through the returned data using:
for (var i=0; i<data.d.results.length i++) {
var id data.d.results[i].Employee.Id
var emp data.d.results[i].Employee.Title
}
DESIRE:
I'd like to dynamically pass a field name, such that I can do something like:
var emp = data.d.results[i].[pField].Title
PROBLEM:
This Bracket Notation, nor any variation, works.
QUESTION:
Is it possible to dynamically reference this object name, returned by the $Expand [object], and if so, what is the proper format?
Thanks,
Steve Clark - Easy Bins Dumpster Rentals | http://www.easy-bins.com

Related

Unable to retrieve custom list value from saved search in netsuite

Creating saved search in suitescript using nlapiSearchRecord. All the column value returns except one column which is type is custom list.
How could I get value of custom list?
To get the value I'm using code lines below.
columns[0] = new nlobjSearchColumn( 'customlist' );
var searchresults = nlapiSearchRecord( 'customrecord', null, filters, columns );
To get the column value
var listValue = searchresult.getListValue( 'customlist' );
I assume you've simplified your code in trying to be clear or confidential but there will never be fields or records with those ids.
from a search you would do:
var searchResult = searchResults[0];
searchResult.getValue(fieldId, joinName, summary)
// or in your case
searchResult.getValue('customlist'); //returns id of list value or simple result of non-list/record fields
or (and I think this is the one you want)
searchResult.getText('customlist'); // returns the display value of the list/record field.

How to get from CouchDB only certain fields of certain documents for a single request?

For example I have a thousands of documents with same structure, for example:
{
"key_1":"value_1",
"key_2":"value_2",
"key_3":"value_3",
...
...
}
And I need to get, let's say key_1, key_3 and key_23 from some set of documents with known IDs, for example, I need to process only 5 documents while my DB contains several thousands. Each time I have a different set of keys and document IDs. Is it possible to get that information for a one request?
You can use a list function (see: this, this, and this).
Since you know the ids, you can then query _all_docs with the list function:
POST /{db}/_design/{ddoc}/_list/{func}/_all_docs?include_docs=true&columns=["key_1","key_2","key_3"]
Accept: application/json
Content-Length: {whatever}
{
"keys": [
"docid002",
"docid005"
]
}
The list function needs to look at documents, and send the appropriate JSON for each one. Not tested:
(function (head, req) {
send('{"total_rows":' + head.total_rows + ',"offset":' + head.offset + ',"rows":[');
var columns = JSON.parse(req.query.columns);
var delim = '';
var row;
while (row = getRow()) {
var doc = {};
for (var k in columns) {
doc[k] = row.doc[k];
}
row.doc = doc;
send(delim + toJSON(row));
delim = ',';
}
send(']}');
})
Whether this is a good idea, I'm not sure. If your documents are big, and bandwidth savings important, it might.
Yes, that’s possible. Your question can be broken up into two distinct problems:
Getting only a part of the document (in your example: key_1, key_3 and key_23). This can be done using a view. A view is saved into a design document. See the wiki for more info on how to create views.
Retrieving only certain documents, which are defined by their ID. When querying views, you cannot only specify a single ID (or rather key), but also an array of keys, which is what you would need here. Again, see the section on querying views in the wiki for explanations and examples.
Even though you only need a subset of values from a document, you may find that the system as a whole performs better if you just ask for the entire document then select the values you need from that result.
To only get the specific key value pairs you need to create a view that has view entries with a multipart key consisting of the doc id and doc item name, with value of the corresponding doc item.
So your map function would look something like:
function(doc){
for(var i = 1; i < doc.keysInDoc; i++){
var k = "key_"+i;
emit([doc._id, k], doc.[k]);
}
}
You can then use multi key lookup with each key being of the form ["docid12345", "key_1"], ["docid56789", "key_23"], etc.
So a query like:
http://host:5984/db/_design/design/_view/view?&keys=[["docid002","key_8"],["docid005","key_7"]]
will return
{"total_rows":84,"offset":67,"rows":[
{"id":"docid002","key":["docid002","key_8"],"value":"value d2_k8"},
{"id":"docid005","key":["docid005","key_12"],"value":"value d5_k12"}
]}

How to assign a value to an Orchard ContentPickerField from code?

I am working on an Orchard site that needs to be able to use some customized forms to create new content items.
To handle this I'm using a controller to display a form and then trying to create the new content items on post back by populating the dynamic items and then sending them through the ContentManagerService's Create() function.
This is working ok until I got to the content picker field I have as part of my content item.
In my project I have a content type of Question Record that has a SubmittedBy field that is a Content Picker Field.
Here is what I can see in the immediate window while processing the post back:
> dynamic q = _questionService.NewQuestion("Why doesn't this work?");
{Custom.Website.Models.Question}
base {Orchard.ContentManagement.ContentPart}: {Custom.Website.Models.Question}
IsNew: true
OriginalQuestion: "Why doesn't this work?"
Summary: null
> q.QuestionRecord
{Orchard.ContentManagement.ContentPart}
base {System.Dynamic.DynamicObject}: {Orchard.ContentManagement.ContentPart}
ContentItem: {Orchard.ContentManagement.ContentItem}
Fields: Count = 5
Id: 0
PartDefinition: {Orchard.ContentManagement.MetaData.Models.ContentPartDefinition}
Settings: Count = 0
TypeDefinition: {Orchard.ContentManagement.MetaData.Models.ContentTypeDefinition}
TypePartDefinition: {Orchard.ContentManagement.MetaData.Models.ContentTypePartDefinition}
Zones: {Orchard.UI.ZoneCollection}
> q.QuestionRecord.SubmittedBy
{Orchard.ContentPicker.Fields.ContentPickerField}
base {Orchard.ContentManagement.ContentField}: {Orchard.ContentPicker.Fields.ContentPickerField}
ContentItems: null
Ids: {int[0]}
The ContentItems property is read-only and the Ids when assigning a new int[] to the Ids array I get a System.ObjectDisposedException with the message: Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.
Are there any workarounds to get this value set in code or do I need to create my own property to store the related content item ids? It would be very helpful to have the admin interface of the ContentPickerField also available.
Thanks.
If you have a reference to the ContentPickerField, you can assign it a value using the Ids property.
In example (assuming your content type has a part called Question which has a field called SubmittedBy):
var submittedByField = ((dynamic)q.ContentItem).Question.SubmittedBy;
sbmittedByField.Ids = new[] { submittedById };
As Bertrand mentioned, the format to access a content field is: contentItem.PartName.FieldName.
If you attached a field to a type directly via the admin, the part name has the same name as the type name, hence contentItem.TypeName.FieldName (where TypeName is actually the name of the implicitly created part).

Pass table name a parameter to the Data Context method using LINQ to SQL in C#

I have created a method which delete data from the database by joining a db column with a csv file, I have a list of tables in a Data Context which I need to iterate, i means that I need to change the table name by passing it as parameter to the method.
Basically I need to use this method for all the table present in the Data Context
Current hard coded Table name in a GetTable method: dw_Job
DataContext Object: job
Here is my code
var query = from line in File.ReadAllLines(file, Encoding.GetEncoding(1252))
//let jobrecord = line.Split(',')
join j in job.GetTable<dw_Job>() on
Convert.ToInt32(line.Split(',').GetValue(factColPos))
equals j.JobID
select j;
foreach (var deletejob in query)
{
Console.WriteLine(deletejob);
job.dw_Jobs.DeleteOnSubmit(deletejob);
}
job.SubmitChanges();
I can able to provide if you need any further details of my code. Please correct me on this
Thanks in advance

Is it possible to use the search results of one search as the criteria for a new search in NetSuite

Using NetSuite is it possible to embed a search within another search? I have a search that I need that will be effectively using another search's results in the criteria.
The basic structure of my search is:
Return all non-inventory skus, starting with a specific prefix,
Where the occurrence of the previously mentioned skus on a custom field on
Inventory-Part records is greater than 0.
This is then intended to be used for alerts
I'm not sure how to build this within NetSuite's search builder.
I don't think this pertains to any scripting as m_cheung suggested.
To answer your question, yes this is doable via saved search.
Transaction > Management > Saved Search > New
Select 'Item' from the list
In the criteria section:
Type = 'Non-Inventory Items'
External ID = starts with (...your desired prefix) (NOTE: Assuming that prefix is the external ID from your question)
Select the Custom field and criteria is greater than 0.
Save and Run to confirm if this is the desired result.
using nlapiSearchRecord(RECORDTYPE, JOIN_, __SEARCHFILTERSARRAY, __SEARCHCOLUMNSARRAY) you can return the results of a search and pass the returned data further into script logic
for example if you build search1 using a searchFilter array and a searchColumn array then pass these arrays into nlapiSearchRecord('item'), you can assign this call to a variable:
var searchresults = nlapiSearchRecord('item', null, searchFiltersArray, searchColumnsArray);
then using searchresults (which is an nlobjSearchResults object) you can pull out your returned search data for criteria in search2:
if(searchresults)
{
for(i=0;i<searchresults.length; i++)
{
var search2FilterAndColumnData = searchresults[i].getAllColumns();
}
}
You can use a saved search for creating another search in suitescript.
Somewhat like ,
var arrSearchResult = nlapiSearchRecord( null , SAVED_SEARCH_ID , FILTERS , COLUMNS);

Resources