Question

This question is about a SharePoint List. The code is using the SPServices, jQuery, and JavaScript.

Some fields for the columns are lookup fields, and I am getting both the ID and the value of the field. Is there a way to filter this out so that I am only getting the value without the ID?

For example, for the AssignedTo field the query returns values such as "91;#Doe, John" and "103;#Doe, Jane" where as what I need to see is "Doe, John", "Doe, Jane", etc.

Any advice on how to fix this would be much appreciated. Thanks!

This is the CAML query segment of the code:

function loadPrioritizedList() {
        $("#tasksUL").empty();
        $().SPServices({
            operation: "GetListItems",    
            webURL: myURL,
            listName: targetListName,
            CAMLViewFields: "<ViewFields><FieldRef Name='Priority_x0020_Number' /><FieldRef Name='Edit_x0020_Link' /><FieldRef Name='Priority' /><FieldRef Name='Top_x0020_Item_x003f_' /><FieldRef Name='Purpose' /><FieldRef Name='Item_x002d_Task_x0020_Order' /><FieldRef Name='Mode' /><FieldRef Name='Work_x0020_Status' /><FieldRef Name='DueDate' /><FieldRef Name='Task_x0020_Type' /><FieldRef Name='DAK_x0020_Date' /><FieldRef Name='DAK_x0020_No' /><FieldRef Name='AssignedTo' /><FieldRef Name='Money_x0020_Estimate' /><FieldRef Name='ItemStatus' /><FieldRef Name='Assign_x0020_Date' /></ViewFields>",
            CAMLQuery: '<Query>' +
    "<Where><IsNull><FieldRef Name=Assign_x0020_Date' /></IsNull></Where>" +
            '<OrderBy>' +
            '<FieldRef Name="Priority_x0020_Number" />' +
            '</OrderBy>' +
            '</Query>', 
        CAMLRowLimit: listrowlimit,  
        completefunc: function (xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function() {
                var tdHtml = "<tr class='sortable_row' id=" + $(this).attr("ows_ID") + ">";
                tdHtml = tdHtml + "<td style=\"width:60px;\">" + PriorityFormat($(this).attr("ows_Priority_x0020_Number"));  + "</td>";
                tdHtml = tdHtml + '<td style=\"width:49px;\"><a href=\"'+($(this).attr("ows_Edit_x0020_Link")).split(", ")[1] + '\">' + ($(this).attr("ows_Edit_x0020_Link")).split(", ")[1] + '</a></td>';
                tdHtml = tdHtml + "<td style=\"width:83px;\">" + $(this).attr("ows_Priority") + "</td>";
                tdHtml = tdHtml + "<td style=\"width:63px;\">" + TopItem($(this).attr("ows_Top_x0020_Item_x003f_")) + "</td>";
                tdHtml = tdHtml + "<td style=\"width:300px;\">" + StringChk($(this).attr("ows_Purpose")) + "</td>";
                tdHtml = tdHtml + "<td style=\"width:125px;\">" + StringChk($(this).attr("ows_Item_x002d_Task_x0020_Order")) + "</td>";                     
                tdHtml = tdHtml + "<td style=\"width:40px;\">" + StringChk($(this).attr("ows_Mode")) + "</td>";
                tdHtml = tdHtml + "<td style=\"width:75px;\">" + StringChk($(this).attr("ows_Task_x0020_Type")) + "</td>";
                tdHtml = tdHtml + "<td style=\"width:150px;\">" + StringChk($(this).attr("ows_Work_x0020_Status")) + "</td>";
                tdHtml = tdHtml + "<td style=\"width:100px;\">" + FormatDate($(this).attr("ows_DueDate")) + "</td>";
                tdHtml = tdHtml + "<td style=\"width:100px;\">" + FormatDate($(this).attr("ows_DAK_x0020_Date")) + "</td>";
                tdHtml = tdHtml + "<td style=\"width:100px;\">" + StringChk($(this).attr("ows_DAK_x0020_No")) + "</td>";
                tdHtml = tdHtml + "<td style=\"width:300px;\">" + StringChk($(this).attr("ows_AssignedTo")) + "</td>";
                tdHtml = tdHtml + "<td style=\"width:125px;\">" + $(this).attr("ows_Money_x0020_Estimate") + "</td>";
                tdHtml = tdHtml + "<td style=\"width:75px;\">" + StringChk($(this).attr("ows_ItemStatus")) + "</td>";
                tdHtml = tdHtml + "<td style=\"width:100px;\">" + FormatDate($(this).attr("ows_Assign_x0020_Date")) + "</td>";
                tdHtml = tdHtml + "</tr>";
                $("#tasksUL").append(tdHtml);
            });
Was it helpful?

Solution

In my framework I use this function (I changed it a bit to make it independant):

/**
@name cleanResult
@function
@description clean a string returned by a GET (remove ";#" and "string;#" and null becomes "")
@param {String} str The string to clean
@param {String} [separator=";"] When it's a list we may want to have a different output (see examples)
@return {String} the cleaned string

@example
cleanResult("15;#Paul"); // -> "Paul"
cleanResult("string;#Paul"); // -> "Paul"
cleanResult(";#Paul;#Jacques;#Aymeric;#"); // -> "Paul;Jacques;Aymeric"
cleanResult(";#Paul;#Jacques;#Aymeric;#", ", "); // -> "Paul, Jacques, Aymeric"
*/
function cleanResult(str,separator) {
      if (str===null || typeof str==="undefined") return "";
      return (typeof str==="string"?str.replace(/;#[0-9]+;#/g,separator).replace(/^[0-9]+;#/,"").replace(/^;#|;#$/g,"").replace(/;#/g,separator).replace(/string;#/,""):str);
    }

OTHER TIPS

This is the way SharePoint returns the value. I would just use a regular expression to replace the value

var newValue = value.replace(/^\d+;#/,"");

This will look at the beginning of the string for a number followed by a semicolon and hashmark, and replace it with nothing.

Using your value above

var value = "91;#Doe"
var newValue = value.replace(/^\d+;@/,"");

will remove 91;# and assign Doe to newValue.

You can use the regular expression as suggeted by Robbert or you can simply split the returned output value using the ;#

In this way you can get the value in an array For example

you can do something like this

string[] words = value.Split(';#');

string NewValue = words[1];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top