I need to do this in JavaScript since the entire solution is current in JavaScript and this is the last part.
I need to be able to update a reply message (comment) to an existing discussion. I am able to change the discussion fields but not the message fields. I know the message and the discussion are two different content types and that the reply messages are under a folder for the discussion but I don't know how to edit the reply message. (There is a utility to add the reply message but not to edit it).
This is a sample of the discussion (in the list) in which you can see there are 5 replies, I want to change the body text of one of the replies via JavaScript.
Image of the Discussion Showing Replies I would like to update
And for example, I want to change the message below:
Image of Replies that I want to change the body text
I have tried to update using this code, but it only changes the discussion and not the message.
I have a feeling I need to tell the system to go into that folder to find the message and change its body text, but I am not sure how to do this and after a 2 day search on the interwebs, I can't find an answer.
Code that does not work:
function aeditListItem() {
var clientContext = new SP.ClientContext();
var oList = clientContext.get_web().get_lists().getById('40b2fbd4-4f87-d92fb05f8044'); //ID changed to protect client
this.oListItem = oList.getItemById(getParameterByName('commentid'));
oListItem.set_item('Body', document.getElementById("ideaDetails").value.replace(/\r?\n/g, '<br />'));
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded() {
alert('Item Updated: ' + oListItem.get_id());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
Many Many THANKS!
Apparently at the line:
this.oListItem = oList.getItemById(getParameterByName('commentid'));
getParameterByName('commentid') does not return the proper message id, make sure the message list item id is specified.
As a proof of concept the following example shows how to:
find a message by body
replace the message with a new body
Example
var listTitle = "Discussions";
var oldMessageBody = "";
var newMessageBody = "";
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(createMessageFindQuery('Body',oldMessageBody));
ctx.load(items);
ctx.executeQueryAsync(
function(){
if(items.get_count() == 1){
var foundItem = items.getItemAtIndex(0);
foundItem.set_item('Body',newMessageBody);
foundItem.update();
ctx.executeQueryAsync(
function(){
console.log("Updated");
},
function(sender,args){
console.log(args.get_message());
});
}
else
console.log('Not found or multiple items are found')
},
function(sender,args){
console.log(args.get_message());
});
});
function createMessageFindQuery(fieldName,fieldVal){
var qry = new SP.CamlQuery;
qry.set_viewXml(String.format('<View Scope="RecursiveAll"><Query><Where><Contains><FieldRef Name="{0}" /><Value Type="Text">{1}</Value></Contains></Where></Query></View>',fieldName,fieldVal));
return qry;
}
Related
Using Javascript in SharePoint, I need to create a list item in listA in a siteA, then create an identical list item in a listB in siteB.
Both lists would use same content type (with the same number of columns).
If an item is created in listA for the first time, then listB would create the same item and after that only update the same item using an ID column.
This is the code I have written so far:
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(updateListItem, "sp.js");
function updateListItem() {
var siteURL = "https://phessp.webhc.hctx.net/pmo/projectsitetemplate";
var ctx = new SP.ClientContext(siteURL);
var oList = ctx.get_web().get_lists().getByTitle('Project Information');
//ProjectID is the field in current list, I am using this ID to locate the same record with same ProjecdID for update
var itemId = toString(GetUrlKeyValue('ProjectID'));
this.oListItem = oList.getItemById(itemId);
oListItem.set_item('Timeline_x0020_Description', 'My Updated Timeline');
oListItem.update();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert('Item updated!');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
I tried multiple approaches but am having issues.
I'm working on a project where I have a twitter bot that reply every tweets in its acount.
Here's my code:
var replybot = function() {
//The word that we are going to search in tweets
var word = 'hello';
//Variables to store the twitter user id and screen name to make a reply
var id_str, screen_name;
console.log('Bot started looking for the word ' + word + '.');
stream.on('tweet', tweetEvent );
function tweetEvent(tweet) {
var info_text = tweet.text;
if (info_text.indexOf(word) > -1) {
console.log(tweet.text); //Displays the tweet with the word
//We store the twitter id and the user screen name to make a reply
id_str = tweet.id_str;
screen_name = tweet.user.screen_name;
console.log('need do it once');
//Now we are going to reply the tweet
Twitter.post('statuses/update', {in_reply_to_status_id: id_str,
status: '#' + screen_name + ' I think you mean "goodbye"'},
function(error, tweet, response){
if(error) {
console.log(' Error');
}
else{
console.log('. Success!!!');
} // Tweet body
});
}
}
console.log('done');
}
replybot();
// 'reply' a tweet in every 25 minutes
setInterval(replybot, 1500000);
Im following this repo to work: https://github.com/ttezel/twit .
But i had an issue : when i run this code above, the bot reply so many time on one tweet, and i dont know why.
Although i've set interval for this function.
Im new with node and any help would be great ! Thanks
Oh so sorry i got a loop forever in here :
function(error, tweet, response)
it shouldn't be tweet arg
I'm trying to show a notification using an image I receive over a socket as an arrayBuffer. The notification shows, but without the image supplied. The standard firefox logo/icon is used instead. Any help would be appreciated. The code seems to run without any errors, or rather, no errors are are thrown/stack trace printed when the notification is created.
Here is the code to create the notification:
ps_worker.port.on("notification", function(notification){
//DISPLAY LINK TO USER
var arrayBuffer_icon = notification.icon;
var arrayBuffer_largeicon = notification.largeicon;
var str = String.fromCharCode.apply(null,arrayBuffer_icon);
var base64String = utils.btoa(str).replace(/.{76}(?=.)/g,'$&\n');
var dataUri = "data:image/png;base64,"+ base64String;
notifications.notify({
title: notification.app + ": " + notification.title,
text: notification["subject"] + "\n" + notification.content,
data: "did gyre and gimble in the wabe", // data is a string passed through to the on click listener
iconURL: dataUri,
onClick: function (data) {
console.log(data);
}
});
});
the utils.btoa call is implemented as described here: https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Unit_testing
The relevant server code is (node.js):
function send_notification_to_socket(user, notification, target){
fs.readFile(notification.iconpath, function(err, buf){
if(socketstore.get_socket_by_id(user)){
socket = socketstore.get_socket_by_id(user);
notification["icon"] = buf;
socket.emit('notification', notification);
}else{
console.log("No socket for user " + user);
}
});
}
Any ideas on what I might be doing wrong?
I have a Google spreadsheet with a script that checks a local library catalog to see if an ISBN-searched book has shown up or not. There's no API or RSS feature so I'm using UrlFetchApp to screen scrape for certain unique phrases that appear in the HTML source for a given book status.
Right now my spreadsheet only updates the "Status" column (which invokes the script based on an ISBN field) when I open the spreadsheet. Is there a way I can have Google Script run my main function and then trigger for changes? The documentation I've read has only mentioned user edits, which is not what I'm looking for.
function ISBNsearch(ISBN){
var consortURL = "http://LIBRARYURL/search~S6/?searchtype=i&searcharg=" + ISBN + "&searchscope=4";
var retrieveURL = UrlFetchApp.fetch(consortURL).getContentText();
var searchURLno = retrieveURL.search("BROWSE SCREEN TABLE");
var searchURLyes = retrieveURL.search("Item Location");
var searchURLordered = retrieveURL.search("copy ordered for");
if (searchURLno > -1){
var answer = "Not found"
} else if (searchURLyes > "-1") {
answer = "in Consort"
} else if (searchURLordered > "-1") {
answer = "on order"
} else if ((searchURLno == "-1") && (searchURLyes == "-1") && (searchURLordered == "-1")) {
answer = "no input"; }
return answer;
}
function testScript(){
var response = ISBNsearch("9782711802036");
Logger.log("The answer is " + response);
return response;
}
You can use Time Driven triggers to schedule function executions.
You can also programmatically manage these triggers.
I want to create a copy of document in the SharePoint document library.
Basically let us assume there is a template and every user will open the document by clicking on it. I want to create a copy of file user has clicked and open that file for editting.
I have tried using JavaScript Client Object model of SharePoint. But the examples are for manipulating list items but not for document library.
Can any one please point to any sources that I can use to manipulate the files in document library
One restriction being I need to use JavaScript object model or web services to achive this functionality. i.e., NO server side code
Following is the code I got till now
The approach I am planning to use is copy the existing file object
Rename it and
Save it to other document library
Please ignore formatting as I am not able to do it properly and this is under development code
<script type="text/javascript">
var clientContext = null;
var web = null;
var meetingItems = null;
var filePath = null;
var file = null;
debugger;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize() {
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle("Documents");
clientContext.load(list, 'Title', 'Id');
var queryStart = "<View>"+ "<Query>"+ "<Where>"+ "<Eq>"+ "<FieldRef Name='Title'/>" + "<Value Type='Text'>";
var queryEnd = "</Value>"+ "</Eq>"+ "</Where>"+ "</Query>"+ "</View>";
camlQuery = new SP.CamlQuery();
queryMeeting = queryStart + 'DevCookbook'+ queryEnd;
camlQuery.set_viewXml(queryMeeting);
meetingItems = list.getItems(camlQuery);
clientContext.load(meetingItems);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListLoadSuccess), Function.createDelegate(this, this.onQueryFailed));
}
function onListLoadSuccess(sender, args) {
filePath = meetingItems.get_item(0).get_path();
file = meetingItems.get_item(0);
debugger;
clientContext.load(file);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onFileLoadSuccess), Function.createDelegate(this, this.onFileFailed));
// alert("List title : " + this.list.get_title() + "; List ID : " + this.list.get_id());
// doclist();
}
function doclist()
{
var path = file.get_title();
path = meetingItems.get_item(0).get_file().get_title();
}
function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
function onFileLoadSuccess(sender, args) {
debugger;
alert("List title : " + this.list.get_title() + "; List ID : " + this.list.get_id());
}
function onFileFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
I used copy webservice to do the functionality.
Approach is combination of Object Model and JavaScript functions
Copy the file from templates library.
Check out file using "CheckoutDocument" function
Add metadata in background
Show edits metadata pop up to user using
var oDialog = {
url: "../Library/Forms/Edit.aspx?ID=" + itemID,
title: "Create a new document"
};
SP.UI.ModalDialog.showModalDialog(oDialog)
After user input check in the document