GetListItems "Author" format

Dec 13, 2011 at 3:53 PM

Right now, my output for the column "Author" is:    1;#(name)

Is it possible to remove the "1,#" before the name?  I would like to display just the name of that Author.

Thanks

Coordinator
Dec 13, 2011 at 3:55 PM

You can use substring functions to reformat the person column values.

M.

Dec 13, 2011 at 4:12 PM

I tried:

$(this).attr("ows_Author", str.substring(3))

But I think this is incorrect. Any thoughts?

Coordinator
Dec 13, 2011 at 4:17 PM

I'm not sure where the srt variable is coming from, but it's a little more complicated than what you are doing. The user might look like 1;#boo or 12324354;#boo. You need to find the ';#' and take the characters after that. Something like this (untested - I'm just typing it here).

var author = $(this).attr("ows_Author");
author = author.substring(author.indexOf(";#"));

M.

Dec 13, 2011 at 9:06 PM

 

Sympmarc,

If you have some time, could you help debug this script for me?  It is returning "[object Object]". I bold the code that I am having issues with.

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" language="javascript"  src="../SiteAssets/Scripts/jquery.SPServices-0.6.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Recent Updates",
    CAMLViewFields: "<ViewFields Properties='True' />",    
	completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName='z:row']").each(function() {
      	var author = $(this).attr("ows_Author");
      	author = author.substring(author.indexOf(";#"));
        var liHtml = "<li>" + "<strong>" + $(this).attr("ows_Title") + "</strong>" + "<br>" + $(this).attr("ows_Created") + " by " + $("#author").text(author) + "</li>" + "<br>";
        $("#recentupdatesUL").append(liHtml);
        $("strong").css({"color":"#0a2463","font-size":"12px", "font-weight":"bold"});
      });
    }
  });
});
</script>
<ul id="recentupdatesUL"/>


 

 

Dec 14, 2011 at 3:44 PM

Try this:

var author = new String($(this).attr("ows_Author"));
author = author.substring(author.indexOf("#") + 1);

This should work. You may have to remove the + 1 part, but it looks right based on how indexOf works.

Dec 14, 2011 at 4:57 PM

Note that your issue has nothing to do with SPServices, but rather is just a javascript "how do I" question

Here is also a function I often use to get the "visible value" of a Lookup field:

/**
 * FUNCTION: getLookupFieldValue
 * 
 * 		Returns the value of lookup field.
 * 		SP returns lookup field values specially coded with as "<id>;#<value>".
 * 		This function takes that value and return only the "real" value or, if
 * 		the input param "returnID" is set, it return the <id>.
 * 
 * 
 * PARAMS:
 * 
 * 		@param {Object} xmlFieldValue	-	The field value as found in XML response
 * 							message for a lookup field.
 * 		@param {Boolean} returnID	-	If set to true, then instead of the lookup
 * 							field's value, this method will return its
 * 							ID.
 * 
 * REUTRN:
 * 
 * 		@return {String} fieldValue
 * 
 */
function getLookupFieldValue(xmlFieldValue, returnID){
	if (xmlFieldValue == undefined) {
	     return "";
	}
	if (!xmlFieldValue || !xmlFieldValue.indexOf("#")) {
	     return xmlFieldValue;
	}
	var field = xmlFieldValue.substring(xmlFieldValue.indexOf("#") + 1);
	if (returnID) {
	     field = parseInt(xmlFieldValue.substring(0, xmlFieldValue.indexOf(";")));
	}
	return field;
}/* getLookupFieldValue() */

It actually handle returning either the ID or the visible value.

 

Paul.

 

 

Dec 14, 2011 at 5:41 PM

@ptaveres, 

I thought so but I used an example from SPservices, 'GetListItems'.  And I'm stuck with trying to get that 'Author' field pull into my site without having the '1,#' attached with the author's name.

@spevilgenius

I put in your code. And it is returning [object Object] for 'author' in the var liHtml output .  I don't know that means. Any thoughts?

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" language="javascript"  src="../SiteAssets/Scripts/jquery.SPServices-0.6.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Recent Updates",
    CAMLViewFields: "<ViewFields Properties='True' />",    
	completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName='z:row']").each(function() {
      	var author = new String($(this).attr("ows_Author"));
	author = author.substring(author.indexOf("#") + 1);
       
	 var liHtml = "<li>" + "<strong>" + $(this).attr("ows_Title") + "</strong>" + "<br>"
	$(this).attr("ows_Created") + " by " + $("#author").text(author) + "</li>" + "<br>";
        $("#recentupdatesUL").append(liHtml);
        $("strong").css({"color":"#0a2463","font-size":"12px", "font-weight":"bold"});
      });
    }
  });
});
</script>
<ul id="recentupdatesUL"/>

Dec 14, 2011 at 5:52 PM

Try alerting the author variable alert(author); and just use the variable in lieu of $("#author").text(author) because that is an object when done that way.

So change this: $(this).attr("ows_Created") + " by " + $("#author").text(author) + "</li>" + "<br>";

to $(this).attr("ows_Created") + " by " + author + "</li>" + "<br>";

Dec 15, 2011 at 12:35 PM
Edited Dec 15, 2011 at 12:42 PM

spevilgenius is right, your syntax is wrong when trying to add the author to your liHtml variable. You can also save space by applying string alterations inline instead of creating a variable first. I think this is the best way to do it unless you're planning on reusing that variable, obviously.

You can replace:

var author = $(this).attr("ows_Author");
author = author.substring(author.indexOf(";#"));
var liHtml = "<li>" + "<strong>" + $(this).attr("ows_Title") + "</strong>" + "<br>" + $(this).attr("ows_Created") + " by " + $("#author").text(author) + "</li>" + "<br>";

with

var liHtml = "<li><strong>" + $(this).attr("ows_Title") + "</strong><br />" + $(this).attr("ows_Created") + " by " + $(this).attr("ows_Author").split(";#")[1] + "</li><br />";

Notice how I use the javascript split method. This returns an array with the values on either side of the ";#" seperator, so adding square brackets with the index gives us the string we're looking for.

$(this).attr("ows_Author").split(";#")[0] returns the userid of the author (Yes, that first number actually means something)

and

$(this).attr("ows_Author").split(";#")[1] returns the author's name, which is what you're looking for.

Dec 15, 2011 at 2:43 PM

Yes, that is another good way to do it. I did not assume usage later of the variable, but that is a good way to handle it.

Dec 15, 2011 at 3:45 PM

Thanks!  This works perfectly.  I learned something new today. Thanks again guys.

I have one last question.  Is there also a inline way to change a format of the date in "created" column ?  This my question earlier in another dicussion: http://spservices.codeplex.com/discussions/282802