I'm trying to get list items from a SharePoint list with specific fields only.
The list contains lookup fields to other lists and I need the LookupId only for these.
I know two cases to narrow down the returned fields.
The first is with CAML. I can use the LookupId="TRUE" option to suppress the LookupValue in this case, but the server returns other system fields (eg. Modified By, Created By, etc.) which I don't need to.
The second is using 'Include' in ClientContext.load method. In this case the server not sends the system fields but I don't know how to specify (if possible) to receive the LookupId only for lookup fields.
I tried several versions in Include (project.id, projectId, project.lookupId, project.Inlude(id)) but none of them worked.
So, my question is: how to retrieve only specified fields of a SharePoint list with LookupIds only for lookup fields?
You were very close to archive the desired result, you need to combine two techniques in order to retrieve id part of lookup value as demonstrated below:
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(createLookupQuery('Project'));
ctx.load(items,'Include(Project)');
ctx.executeQueryAsync(
function(){
if(items.get_count() > 0){
var item = items.getItemAtIndex(0);
console.log(item.get_fieldValues()['Project'].get_lookupId());
}
},
function(sender,args)
{
console.log(args.get_message());
});
function createLookupQuery(lookupFieldName){
var qry = new SP.CamlQuery();
qry.set_viewXml("<View><ViewFields><FieldRef Name='" + lookupFieldName + "' LookupId='TRUE' /></ViewFields></View>");
return qry;
}
In CAML query we specify attribute LookupId='TRUE' to retrieve Id
part of lookup field value
since CAML query includes also system field values, we utilize
SP.ClientContext.load method to return specific field value
only
how to retrieve only specified fields of a SharePoint list with
LookupIds only for lookup fields?
The question is a bit unclear, but assuming you want to fetch the lookup id, you can do as follows.
Suppose you've retrieved the SP.ListItem with JSOM and stored it in to a variable, your lookup field will be representeed by a SP.LookupFieldValue object.
You can access it from the retrieved item object as below.
var lookupFieldValue = item.get_item('FieldName');
var lookupId = lookupFieldValue.get_lookupId();
Related
I'm getting a collection from my Firestore database and adding the document values to an array in JS:
var data = myCollection();
var myArray = [];
data.forEach(function(data) {
var splitPath = data.name.split('/');
var documentId = splitPath[splitPath.length - 1];
var name = data.fields.name ? data.fields.name.stringValue : '';
var country = data.fields.vin ? data.fields.vin.stringValue : '';
myArray.push( [ documentId, name, country ] );
});
Suppose I know a document ID, is it possible to get the collection documents from that certain document ID?
I'm not sure if Firestore documents are ordered by date. I am trying to get the most recent documents from a certain document ID.
Suppose I know a document ID, is it possible to get the collection documents from that certain document ID?
When it comes to the Firebase console, all documents are sorted lexicographically by ID and this behavior can't be changed. When it comes to code, it's up to you to choose how to order the results.
I'm not sure if Firestore documents are ordered by date.
No, there is no time component inside the document IDs.
I am trying to get the most recent documents from a certain document ID.
In that case, the simplest solution would be to add a timestamp field in each document and order them according to that field.
However, Firebase Realtime Database pushed IDs do contain a time component. So if you want, you can add that data there. Both databases are working great together.
If you have multiple documents and you want to implement pagination, for example, you can use query cursors and use startAt() or starAfter() if you need that.
I don't know if this is exactly what you need but firebase docs has below example:Order and limit data
import { query, where, orderBy, limit } from "firebase/firestore";
const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
if you adjust where part to your id, then sort by date it should work.
I have an Add-on I'm updating for Sheets. I want to store information each time the user runs one of two functions, so I've created a function to push than info to Document Properties. Ultimately I want to send that data to a sheet at my end once a certain number of values have been collected.
The problem I'm having is that when I run a test function to Log the data contained, it only shows me the most recent data; I'm not sure I'm adding to existing data or replacing it. The data pairs should include the user's email address and the name of the sheets tab created by one of two functions that call this storeStats function.
In short:
*Do I need to declare the name of the Property Store before adding data to it?
*How do I add more data without deleting the old?
*How can I check how much data is stored? I'm thinking along the lines of array.length, but not sure if that works in Properties
*I'm assuming I need to use the parse command to retrieve it and send to the sheet at my end. That may wind up in a separate question later, but any ideas are appreciated.
function storeStats(sheetTitle) {
var docProps = PropertiesService.getDocumentProperties();
var userID = Session.getActiveUser().getEmail();
var thisData = {user:userID, sheet:sheetTitle};
var thisDataStr = JSON.stringify(thisData);
var useData = "USEDATA"; //name of the properties store maybe
docProps.setProperties(useData,thisDataStr);
Logger.log(useData.length);
//send when enough values are present
//use parse to extract and send?
// /*if(/*see how many values are in the data set*/) {
//parse values from value set array
//send the whole batch to the range on the collection sheet
//} */
}
No errors are created thus far, but this only returns one email address and one sheet name rather than all values send from previous function calls.
docProps.setProperties(useData,thisDataStr); is not adding data to "USEDATA" if you want to add or append data to it you need to do something like this:
docprops.setProperty('USEDATA', docprops.getProperty('USEDATA') + thisDataStr);
Example:
function propertiesTest() {
var ps=PropertiesService.getScriptProperties();
ps.setProperty('test','');
for(var i=0;i<10;i++) {
ps.setProperty('test',ps.getProperty('test') + '\n' + i)
}
Logger.log(ps.getProperty('test'));
}
I want to update all the fields in a MongoDB document and I have a Javascript object that contains all these fields. I could easily type out each field to update but this seems like a lot of manual work and not reusable. I wanted to do something like below but this creates an object containing all the new field data within the document called newData.
I've tried JSON.stringify on the variable but the format isn't appropriate for update.
var newData = {
_id:ObjectId("53245234..."),
id: 88888,
firstData: "someData",
secondData: 787855,
thirdData: [ 45,17,12,234]
};
var collection = db.get('CollectionToUpdate');
//strip out dB id so as not to overwrite it, possibly not needed
if ("_id" in newData) {
delete newData["_id"];
}
//find the correct document based on program generated id and update
collection.update({id: newData.id}, {
newData
})
If you trust newData will not have any keys you don't intend (like update operators) this should work:
var collection = db.get('CollectionToUpdate');
collection.update({id: newData.id}, newData)
Note that this replaces the document. I assume that is what you meant by "update all the fields". update does not replace "_id".
Documentation for update
I have a form with a lot of inputs and values. I want to push all the data to a firebase database. I could assign variables to each field value as:
let name = $("#field1").val();
let surname = $("#field2").val();
etc etc, but this feels very inefficient.
I want to create a object and loop through all input fields and map their name and value, something like this:
const collection = {};
$('form input').each(function () {
collection[$(this).attr('name')] = $(this).val();
});
Then I want to push all the data to firebase. But how do I push the entire object to firebase?
That's as simple as:
firebase.database().ref().push(collection);
Of if you want it under a specific location/path in the database:
firebase.database().ref("specific/place").push(collection);
Btw: I highly recommend reading the Firebase documentation on reading/writing lists of data.
I checked similar questions such as:
How do I select a collection within a collection using LINQ?
How to return ICollection<object> from linq, when using select new object() {}
What I'm trying to do is return the list in Json format to be used in a Javascript GET request and display it on a cshtml page.
var query = (from c in db.photos
orderby c.date_created descending
select new { id = c.id, name = c.name, image_url = c.image_url, description = c.description, photos_has_characters = c.photos_has_characters.FirstOrDefault() })
.Skip(pageIndex * pageSize)
.Take(pageSize);
return Json(query.ToList(), JsonRequestBehavior.AllowGet);
However, when I go to the page nothing loads or shows an error I can pinpoint, so it has to be the photos_has_characters part in the LINQ.
How do I accomplish displaying those associated entities?
Let me re-clarify.
So I have a character's table and a photos tables with a many to many relationship table called photos_has_characters. I want to also select the character name and id from that photos_has_characters collection and return that information with the select new object. How do I do that?
If I'm missing something I will add it, thank you in advance.
EDIT:
In client-side on console I get this error:
What the LINQ is returning for photos_has_characters parameter...
Although it doesn't return an error, the parameter for new object of the photos_has_characters collection returns this:
photos_has_characters =
{System.Data.Entity.DynamicProxies.photos_has_character_A0E9952F88D75712B3DACC51D4EA09329C0427E5A9FED3F11F4EE0EC2AE1B579}
That into Json to be read using Javascript results in nothing. How do I modify my LINQ to accomodate?