2013-06-27 28 views
1

這個問題是關於一個SharePoint列表。代碼使用SPServices,jQuery和JavaScript。SharePoint列表上的CAML查詢返回ID和值 - 如何獲取值?

列的一些字段是查找字段,我得到的字段的ID和值。有沒有一種方法可以過濾出來,這樣我只能得到沒有ID的值?

例如,對於AssignedTo字段,查詢返回的值如「91; #Doe,John」和「103; #Doe,Jane」,我需要看到的是「Doe,John」,「Doe ,簡「等。

任何關於如何解決這個問題的建議將不勝感激。謝謝!

這是代碼的CAML查詢段:

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); 
      }); 

回答

2

在我framework我用這個函數(我改變了它一下,使其獨立的):

/** 
@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); 
    } 
+0

這工作出色!最近非常感謝你提供給我的問題的所有答案。這對我的項目有很大的幫助。 – WCS

0

這是的SharePoint返回值的方式。我只想用正則表達式替換值

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

這將看串數字後面跟着一個分號hashmark的開始,什麼也沒有更換。

使用你的價值上面

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

將刪除91;#和分配能源部NEWVALUE。

0

您可以使用正則表達式由Robbert作爲suggeted或者您可以使用;#

這樣簡單地拆分返回的輸出值,你可以在一個陣列 獲得的價值。例如

可以做這樣的事情

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

string NewValue = words[1]; 
+0

這也很好。只要確保;#不是原始字符串中的任何其他地方。 – Robbert

+0

問題是JavaScript的;-) – AymKdn