Retrieve email from People or Group Field - javascript

I have People or Group Field
Normally I retrieve value of fields as:
var fieldUsuario = ctx.CurrentItem.Notificar_x0020_a
But now I dont want text value, I debbug it and it have email in span as:
<span class="ms-entity-resolved" id="Notificar_x0020_a_084ffd45-b361-458e-b55f-c824ba8995ec_$ClientPeoplePicker_i:0#.f|membership|email#mydomain.com_ProcessedUser0_UserDisplay" title="User, SubName" style="max-width: 331px;">User, SubName</span>
As you can see it have: email#mydomain.com I want to retrieve this value. How can I achieve it? Regards

You may try this.
<script type="text/javascript">
SP.SOD.executeFunc("sp.js", 'SP.ClientContext', function (){
var clientContext = new SP.ClientContext.get_current();
var oWeb = clientContext.get_web();
var list = oWeb.get_lists().getByTitle('PeopleFieldTest');
var item = list.getItemById(1);
clientContext.load(item);
clientContext.executeQueryAsync(function (sender, args) {
var userEmail = item.get_item("Notificar").get_email();//replace Notificar with your person column
alert(userEmail);
}, function (sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
});
});
</script>

Related

Javascript/jquery - Construct URL from entered form. Lookup and replace

Can anyone help with the below code?
I am trying to pass a vip name from a user entered form;
Example
app-123.dc.domain.com
I have two DC's PAL/PFW I want it so that when a user enters either a PAL or DFW vip my url building part will add both app-123.PAL.domain.com & app123.PFW.domain.com
output example
New winow URL:
http://dashboardbuilder:3000/dashboard/db/omni-vip?var-vip1=app-123.PAL.domain.com&var-vip2=app123.PFW.domain.com
$(window).on('load', function() {
$('#open').click(function() {
var fixedData1 = 'http://dashboardbuilder:3000/dashboard/db/omni-vip?var-vip1=';
var fixedData2 = '&var-vip2=';
function test2() {
var regex = /\.(pfw|pal)\./i;
userEntry1 = $('#one').val(),
pal = userEntry1.replace(regex, '.pal.');
pfw = userEntry1.replace(regex, '.pfw.');
return replaced2;
}
var newWindow = window.open(fixedData1 + pfw + fixedData2 + pal);
newWindow.focus();
});
});

How to get active directory user name without domain name?

I am getting this in my SharePoint output in script editor. executions as follows:
i:0#.w|iscsharepoint\administrator
The code for this is as follows for this
<script type="text/javascript">
function GetLoggedInUserName()
{
var context = new SP.ClientContext.get_current();
this.website = context.get_web();
this.currentUser = website.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args)
{
alert(currentUser.get_loginName());
}
function onQueryFailed(sender, args)
{
alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
}
</script>
Now I am trying to get it this excution.
"Hi Administrator"
You can parse the username manually, like so:
var name = currentUser.get_loginName().split("\\")[1];
alert("Hi " + name);
You can also retrieve the user's actual name instead of their login name:
var name = currentUser.get_title();
alert("Hi " + name);

Why is Incorrect data being returned from query runs?

Using Parse.com and JavaScript SDK.
http://www.kudosoo.com/friendslist.html
Using the code here http://jsfiddle.net/Dano007/LJ8rn/ (just for ref, it wont run).
I'm expecting this to return the uploaded badges of the user that is selected in "friendName" variable after the user clicks on the username.
What I'm seeing happen when clicking on the username, is that
Username.push(object.get("uploadedBy"));
is returning the username of the name they have just clicked and not the actual user name of the person that uploaded the badge.
Therefore I'm concluding that somewhere in my JQuery/JavaScript code I'm some how selecting the wrong element. I presume this must be to do with the FriendName variable? but I can't solve it and its driving me crazy
In the screen shot you can see that its a3aePaphBF that should be returned in
<div id="FriendsStuffName"></div>
not 'A', 'dan' or 'rob'
$(document).ready(function() {
$(document).on('click','.username', function (event) {
event.preventDefault();
friendName = $(this).text();
console.log(friendName);
friendFeed();
});
});
/////////////////
var currentUser = Parse.User.current();
var myBadges = Parse.Object.extend("myBadges");
// Captures the input from the user and checks if the name already exists within the Db.
function friendFeed() {
var friendName = $('#friendsearch').val();
//console.log(friendName);
var query = new Parse.Query(myBadges);
new Parse.Query("myBadges").matchesQuery("uploadedBy", new Parse.Query("_User").equalTo("username", friendName));
query.find({
success: function (rules) {
imageURLs = [];
for (var i = 0; i < rules.length; i++) {
var object = rules[i];
imageURLs.push(object.get("BadgeName"));
Username.push(object.get("uploadedBy"));
}
for(var j = 0; j < imageURLs.length; j++) {
$('#FriendsStuff').append("<img class='images' src='" + imageURLs[j] + "'/>");
$('#FriendsStuffName').append("<div class='username'>'"+Username[j]+"'</div>");
}
},
error: function(error) {
//If the query is unsuccessful, report any errors
alert("Error: " + error.code + " " + error.message);
}
});
}
</script>
<div id="imgs"></div>
<div id="username"></div>
<div id="container"></div>
<div id="FriendsStuff"></div>
<div id="FriendsStuffName"></div>
</body>
</html>
and here are my users
This does not make sense:
var query = new Parse.Query(myBadges);
new Parse.Query("myBadges").matchesQuery("uploadedBy", new Parse.Query("_User").equalTo("username", friendName));
First you are creating a query for "myBadges", assigning it to the variable "query". Then you are creating another query, also for "myBadges". This new query is not assigned anywhere.
The query that you are running the "find" operation on is the one that you have assigned to the query variable.
If you are expecting the second line, to have any influence on the first, then that is likely the source of your error.
Try this:
var query = new Parse.Query(myBadges);
query.matchesQuery("uploadedBy", new Parse.Query("_User").equalTo("username", friendName));
query.find({
success: function (rules) {

Query SharePoint List Using JavaScript

I'm trying to obtain values from a SharePoint 2010 list. The List name is "SitesList" at the URL of "servername/sites/dev/Lists/SitesList/AllItems.aspx". The Columns that I would like to print on the alert message "Title" and "URL."
Once I run everything, the page loads but nothing happens at all. I can see that my web part is there and I even switched out the code to something simple like an alert message and it works. What am I doing wrong?
<script type="text/javascript">
var siteUrl = '/sites/dev/';
function retrieveListItems() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('SitesList');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View><Query><Where><Geq><FieldRef Name=\'ID\'/>" + "<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += '\nID: ' + oListItem.get_id() +
'\nTitle: ' + oListItem.get_item('Title') +
'\nURL: ' + oListItem.get_item('URL');
}
alert(listItemInfo.toString());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
It looks like you're never actually triggering the retrieveListItems function.
Try adding this at the top of you script, after the var siteUrl:
<script type="text/javascript">
var siteUrl = '/sites/dev/';
ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
function retrieveListItems() {
...
}
...
</script>
This is SharePoint specific and will wait for SP.JS to load before executing the retrieveListItems function.
This is typically a more recommended approach than jQuery's document.ready function, or native JS's self executing functions as many things happen behind the scenes with SharePoint apps after the page loads.
Hope this helps!

Create a copy of document in SharePoint 2010 document library using JavaScript

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

Categories

Resources