【问题标题】:CAML Query on a SharePoint list is returning the ID and the value - how to just get the value?SharePoint 列表上的 CAML 查询返回 ID 和值 - 如何获取值?
【发布时间】:2013-06-25 12:32:10
【问题描述】:

这个问题是关于 SharePoint 列表的。该代码使用 SPServices、jQuery 和 JavaScript。

列的某些字段是查找字段,我得到了字段的 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);
            });

【问题讨论】:

    标签: javascript jquery sharepoint caml spservices


    【解决方案1】:

    在我的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);
        }
    

    【讨论】:

    • 这非常成功!非常感谢您最近为我的问题提供的所有答案。它对我的项目有很大帮助。
    【解决方案2】:

    这是 SharePoint 返回值的方式。我只会使用正则表达式来替换值

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

    这将在字符串的开头查找一个数字,后跟一个分号和井号,并将其替换为空。

    使用上面的值

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

    将删除 91;# 并将 Doe 分配给 newValue。

    【讨论】:

      【解决方案3】:

      您可以使用 Robbert 建议的正则表达式,也可以使用 ;# 简单地拆分返回的输出值

      通过这种方式可以获取数组中的值 例如

      你可以这样做

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

      【讨论】:

      • 这也很好用。只需确保 ;# 不在原始字符串中的任何其他位置。
      • 问题是 JavaScript ;-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 2013-01-01
      • 1970-01-01
      • 2020-10-22
      • 2021-10-30
      • 1970-01-01
      相关资源
      最近更新 更多