Can I update a Sharepoint list by grabbing the list via its ID? - sharepoint-2010

I need to populate a large Sharepoint list (about 100 fields) a several fields/a page at a time (when the user navigates away from a page, store the values he entered on that page into the list).
Rather than six different sets of disjointed/unconnected inserts, though, I want the first page to be an insert (generating a new list ID) and the subsequent "inserts" to be actually updates - adding more data to the existing "record" that bears that list ID.
I start off getting a list this way:
var listGUID;
. . .
function createPostTravelListItemTravelerInfo1() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('PostTravelFormFields');
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
listGUID = oList.ID;
// add some field values
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
Can I make the second and subsequent "updates" this way:
var oList = clientContext.get_web().get_lists().getById(listGUID);
...that is to say, by using "getById(listGUID)" instead of "getBytitle('PostTravelFormFields')?
Or will I have to break the list up into multiple lists, one per page, and generate my own GUID, and use that throughout the user's session to tie the lists together (ultimately reading from multiple lists that have the same GUID in common in one of their fields)?

Related

How to create several new records in another SQL table from one button-click

I'm new here. Thanks in advance for your advice.
I’m working on an app which will ask the user how many items they made.
The user will enter a number. My app should then create that many new records in a table called 'Items_Made'.
E.g. The app asks “How many items did you make?”, the user enters “19”, the app then creates 19 new records in the 'Items_Made' table.
I've managed to pull together some code (shown below) that creates ONE new record, but I would like it to create several. I probably need some kind of loop or 'while' function but am unsure how to do so.
var ceateDatasource = app.datasources.Items_Made.modes.create;
var newItem = ceateDatasource.item;
ceateDatasource.createItem();
This code successfully creates 1 record. I would like it to be able to create several.
Creating a lot of records via client script is not recommended, especially if you loose connection or the app gets closed by mistake. In my opinion, the best way to handle this would be via server script for two things: First, It's more reliable and second, it's faster. As in the example from the official documentation, to create a record you need to do something like this:
// Assume a model called "Fruits" with a string field called "Name".
var newRecord = app.models.Fruits.newRecord();
newRecord.Name = "Kiwi"; // properties/fields can be read and written.
app.saveRecords([newRecord]); // save changes to database.
The example above is a clear example on how to create only one record. To create several records at once, you can use a for statement like this:
function createRecordsInBulk(){
var newRecords = [];
for(var i=0; i<19; i++){
var newRecord = app.models.Fruits.newRecord();
newRecord.Name = "Kiwi " + i;
newRecords.push(newRecord);
}
app.saveRecords(newRecords);
}
In the example above, you initiate newRecords, an empty array that will be responsible for holding all the new records to create at once. Then using a for statement, you generate 19 new records and push them into the newRecords. Finally, once the loop is finished, you save all the records at once by using app.saveRecords and passing the newRecords array as an argument.
Now, all this is happening on the server side. Obviously you need a way to call this from the client side. For that, you need to use the google.script.run method. So from the client side you need to do the following:
google.script.run.withSuccessHandler(function(result) {
app.datasources.Fruits.load();
}).createRecordsInBulk();
All this information is clearly documented on the app maker official documentation site. I strongly suggest you to always check there first as I believe you can get a faster resolution by reading the documentation.
I'd suggest making a dropdown or textbox where the user can select/enter the number of items they want to create and then attach the following code to your 'Create' button:
var createDatasource = app.datasources.Items_Made.modes.create;
var userinput = Number(widget.root.descendants.YourTextboxOrDropdown.value);
for (var i = 0; i <= userinput; i++) {
var newItem = createDatasource.item;
createDatasource.createItem();
}
Simple loop with your user input should get this accomplished.

Can I retrieve column/search specific data from a jQuery DataTable?

I am currently working on a page for a web app that displays member data in a jQuery DataTable. I am building a custom plugin for the DataTable that allows for a wide range of filtering per table column.
My current task is to be able to retrieve filtered data from the DataTable, although not updating that data on the table. I know already it is possible to retrieve all filtered data by doing:
var data = $table.dataTable().$('tr', { filter: 'applied' });
This gets data for any text in the search box and any filters applied to columns. I am also aware this call gets the jQuery selectors of cells. That's what I need. But I need more...
My questions are:
Can I get data by only applying what's in the search box, without any other current filters applied to columns? I tried:
var data = $table.dataTable().$('tr', { search: 'applied' });
But that returns the same as { filter : 'applied' }.
Can I target specific columns for getting data, such as:
var data = $table.dataTable().$('tr', { filter: 'applied', columns: [1, 2, 5] });
Can I target specific columns AND what's in the search box?
My plugin needs to able to keep track of data with various combinations of column/search filters applied.
For DataTables 1.9
There is a fnFilter() API method but it applies filtering to the table which is not what you want.
Alternatively you may want to use fnGetData() to get the data for the whole table and filter it yourself.
For DataTables 1.10
There is a search() API method but it applies filtering to the table when used with draw() which is not what you want.
Also there is filter() API method. It is not exactly what you're looking for but very close, however you would need to perform the searching yourself.
You can retrieve the content of the search box as follows:
var searchVal = $('.dataTables_filter input', $table).val();
Then you would need to do the searching yourself, shown below is a simplistic approach which may not match how DataTables perform the search internally.
To search first and second column only, specify [0,1] to columns() method. If no parameters are specified, all columns will be searched instead.
var filteredData = $table.DataTable()
.columns([0, 1])
.data()
.eq( 0 )
.filter( function ( value, index ) {
returrn (value.search(new RegExp(searchVal, "i")) !== -1)
? true : false;
} );
According to the manual, variable filteredData would contain a new API instance with the values from the result set which passed the test in the callback.
To retrieve data for all or selected columns, use columns().data().

How to replace relationship field type object IDs with names / titles in KeystoneJS list CSV download / export?

In Keystone admin list view the handy download link exports all list items in a CSV file, however, if some of the fields are of Relationship type, the exported CSV contains Mongo ObjectIDs instead of nmeaningful strings (name, title, etc) which would be useful.
How can one force the ObjectIDs to be mapped / replaced by another field?
Keystone has an undocumented feature that allows you to create your own custom CSV export function. This feature was added back in April (see KeystoneJS Issue #278).
All you need to do is add a method to the schema called toCSV. Keystone will inject any of the following dependencies when specified as arguments to this method.
- req (current express request object)
- user (currently authenticated user)
- row (default row data, as generated without custom toCSV())
- callback (invokes async mode, must be provided last)
You could, for example, use the Mongoose Model.Populate method to replace the Object Ids of any relationship field with whatever data you want.
Assume you have a Post list with an author field of Types.Relationship to another list (let's say User) which has a name field. You could replace the author Object Id with the author's name (from the User list) by doing the following.
Post.schema.methods.toCSV = function(callback) {
var post = this,
rtn = this.toJSON();
this.populate('author', function() {
rtn.author = post.author.name; // <-- author now has data from User list
callback(null, rtn);
});
};
.toCSV() will be called for every document returned with the Model as the context. When used asynchronously (as above) you should return a JSON representation of the new CSV data by passing it as the second argument of the callback. When using it synchronously simply return the updated JSON object.

How to find Trello ListId's or CardId's with Trello.NET

I'm trying to find all cards in a specific list using Trello.NET
In the examples given it's clear to to find all list names in a Board, and all Cards in a Board. However if I want to find all Cards in a specific List, as far as I can see I need ListID (can't filter by List Name) by using trello.Lists.WithId()
Knowing the List name, how can I determine what it's ListId is so I can subsequently filter for all Cards in it?
And if I would subsequently like to edit an existing Card, how can I determine the CardId.
Use LINQ and filter in-memory:
// More than one list can have the exact same name, this finds the first:
var list = trello.Lists
.ForBoard(board)
.FirstOrDefault(list => list.Name == "name of the list");
if (list != null)
{
var cards = trello.Cards.ForList(list);
// ...
}
You could use a similar technique to find a card if you know it's name: use trello.Cards.ForBoard or trello.Cards.ForList first, and then filter by name afterwards.
For cards there is another option though: trello.Cards.Search. For example:
var cards = trello.Cards.Search("name of the card");
The search will be performed on the server in this case.

TFS API - Why is the AllowedFieldValues for 'Reasons' empty?

I'm trying to retrieve the list of valid Reasons for a WorkItem (MS Agile 5 template), which works correctly for a new work item.
However for editing existing work items, the AllowedValues is always empty, whatever the state.
WorkItem item = GetItem(...)
item.Fields["Reason"].AllowedValues.ToList() // always empty
(ToList is my own extension method).
The problem is, the Visual Studio UI correctly updates the Reasons list when you change the state in the drop down list.
The Reason field also has IsLimitedToAllowedValues=false but when you enter an arbitary value it complains that it's not a valid list item.
We also use MS Agile 5 & the following worked fine on an existing work items named myWorkItem (I tried with User Story & Task):
FieldDefinitionCollection fdc = myWorkItem.Type.FieldDefinitions;
Console.WriteLine(myWorkItem.Type.Name);
foreach (FieldDefinition fd in fdc)
{
if(fd.Name == "Reason")
{
AllowedValuesCollection avc = fd.AllowedValues;
foreach (string allowedValue in avc)
{
Console.WriteLine(allowedValue.ToString());
}
}
}