如何以编程方式获取SharePoint中的审批状态?

时间:2011-04-07 09:44:11

标签: sharepoint lookupfield

我想以编程方式获取SharePoint页面批准状态,我尝试如下

public string GetApprovalStatus(string url,string listName,string fileref)         {             string result = string.Empty;             string caml = @“                                                                                                 “+ fileref + @”                                                           “;

        using (SPSite site = new SPSite(url))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.Lists[listName];
                SPQuery query = new SPQuery();
                query.Query = caml;

                SPListItemCollection myItems = list.GetItems(query);

                if (myItems != null && myItems.Count > 0)
                {
                    DataTable dt = myItems.GetDataTable();
                    result = dt.Rows[0]["_ModerationStatus"].ToString();
                    dt.Dispose();
                }
            }
        }
        return result;
    }

我返回一个号码,如何在文本中获得批准状态?

感谢任何帮助,谢谢你的高级

3 个答案:

答案 0 :(得分:2)

以下代码来自SPModerationInformation.Status的MSDN文章:

using (SPSite oSiteCollection = new SPSite("http://localhost"))
{
  SPWebCollection collWebsites = oSiteCollection.AllWebs;

  foreach (SPWeb oWebsite in collWebsites)
  {
    SPListCollection collLists = oWebsite.Lists;

    foreach (SPList oList in collLists)
    {
        if (oList.BaseType == SPBaseType.DocumentLibrary)
        {
            SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)oList;

            if (!oDocumentLibrary.IsCatalog && oDocumentLibrary.EnableModeration ==
                true)
            {
                SPQuery oQuery = new SPQuery();

                oQuery.ViewAttributes =
                    "ModerationType='Moderator'";

                SPListItemCollection collListItems =
                    oDocumentLibrary.GetItems(oQuery);

                foreach (SPListItem oListItem in collListItems)
                {
                    if (oListItem.ModerationInformation.Status ==
                        SPModerationStatusType.Pending)
                    {
                        Console.WriteLine(oWebsite.Url + "/" +
                            oListItem.File.Url);
                        oListItem.ModerationInformation.Comment =
                            "Automatic Approval of items";
                        oListItem.ModerationInformation.Status =
                            SPModerationStatusType.Approved;
                        oListItem.Update();
                    }
                }
            }
        }
    }
    oWebsite.Dispose();
  }
}

答案 1 :(得分:1)

您可以使用SPModerationStatusType枚举SPModerationStatusType Enum - MSDN来获取所需的文本值。

更多信息:http://spuser.blogspot.com.br/2011/03/how-to-programmatically-get-content.html

答案 2 :(得分:0)

以下是获取和设置(可选)审批状态的完整代码(this.oListItem.get_item('_ ModerationStatus')的可能值:0 - “已批准”,1 - “拒绝”,2-“待处理”) :

<script type="text/javascript" src="/jquery-1.10.2.min.js"></script>
<script src="/jquery.SPServices-2013.02a.js" type="text/javascript"></script>

<script type="text/javascript">


$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(loadConstants, "sp.js"); });    


function loadConstants() {

    var userid= _spPageContextInfo.userId;
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept" : "application/json;odata=verbose" };
    $.ajax({
      url : requestUri,
      contentType : "application/json;odata=verbose",
      headers : requestHeaders,
      success : onSuccess,
      error : onError
    });

    function onSuccess(data, request){
        var loginName = data.d.Title;

        //get current (selected) list item id
        var docurl = document.URL;
        var beginindex = docurl.indexOf('?ID=') + 4;
        var endindex = docurl.indexOf('&Source=');
        var itemid = docurl.substring(beginindex, endindex);

        var ctx = new SP.ClientContext("your site url");
        var oList = ctx.get_web().get_lists().getByTitle('your list name');
        this.oListItem = oList.getItemById(itemid);         

        var appStatus = "";
        ctx.load(this.oListItem);

        ctx.executeQueryAsync(Function.createDelegate(this, function () {  
                                                    //get approval status
                                                    appStatus = this.oListItem.get_item('_ModerationStatus');                                                       

                                                    //set approval status to Approved (0)
                                                    this.oListItem.set_item('_ModerationStatus', 0);
                                                    this.oListItem.update();            

            ctx.executeQueryAsync(
                Function.createDelegate(this, this.onQuerySucceeded), 
                Function.createDelegate(this, this.onQueryFailed)
            );

        }),  function (sender, args) { alert('Error occured' + args.get_message());}); 

    }
    function onError(error) {
        alert("error");
    }       
}

</script>