如何在sharepoint JSOM中获取文档集的AllowedContentTypes

时间:2015-04-27 02:00:32

标签: sharepoint

如何以编程方式获取文档集的AllowedContentTypes属性

在服务器端(C#)我们可以获得如下的CT



var ctIds = ds.ContentTypeTemplate.AllowedContentTypes;
 //1
 foreach (SPContentTypeId ctId in ctIds)
 {
     Console.WriteLine(ctId); //print Content Type Id
 }




如何在Javascript中实现相同我能够获取doc set的文件夹我需要使用哪些属性来获取AllowedContentTypes属性






嗨Vadim感谢您的回答,但我收到了错误" SP.DocumentSet.DocumentSetTemplate'是null或不是对象"



<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script> 
<script type="text/javascript" src="/_layouts/15/sp.js"></script> 
<script type="text/javascript" src="/_layouts/15/sp.documentmanagement.js"></script>

 
<script type="text/javascript">
  // ExecuteOrDelayUntilScriptLoaded(getCT, "sp.js");


SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {
    SP.SOD.executeFunc('sp.documentmanagement.js', 'SP.DocumentSet.DocumentSet', function() {

        var docSetUrl = getQueryStringValue("RootFolder");  //<- Doc Set Url
        getDocumentSetContentTypes(docSetUrl,
           function(ctIds)
           {
               //print content type ids
               ctIds.get_data().forEach(function(ctId){
                    console.log(ctId.get_stringValue());
               });
           },
           logError);
    });   
});


function logError(sender,args){
   console.log(args.get_message());
}

function getFolder(url,success,error)
{
    var ctx = SP.ClientContext.get_current();
    var folder = ctx.get_web().getFolderByServerRelativeUrl(url);
    ctx.load(folder,'ListItemAllFields');
    ctx.executeQueryAsync(
       function(){
          success(folder);
       },
       error);
}

function getDocumentSetContentTypes(folderUrl,success,error)
{
      getFolder(folderUrl,
           function(folder)
           {
               var folderItem = folder.get_listItemAllFields();
               var ct = folderItem.get_contentType();
               var ctx = folder.get_context(); 
               var template = SP.DocumentSet.DocumentSetTemplate.getDocumentSetTemplate(ctx,ct);
               var ctIds = template.get_allowedContentTypes();
               ctx.load(ctIds);
               ctx.executeQueryAsync(
                 function(){
                    success(ctIds);
                 },
                 error);  
           },
           error);
}


	function getQueryStringValue (key) {  
  return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace

(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));  
}


</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

来自SP.DocumentSet.DocumentSetTemplate.getDocumentSetTemplate

sp.documentmanagement.js函数用于获取内容类型模板,get_allowedContentTypes属性返回允许的内容类型,例如:

var template = SP.DocumentSet.DocumentSetTemplate.getDocumentSetTemplate(ctx,ct);
var ctIds = template.get_allowedContentTypes();

以下示例演示了如何使用JSOM

检索文档集的允许内容类型
function getFolder(url,success,error)
{
    var ctx = SP.ClientContext.get_current();
    var folder = ctx.get_web().getFolderByServerRelativeUrl(url);
    ctx.load(folder,'ListItemAllFields');
    ctx.executeQueryAsync(
       function(){
          success(folder);
       },
       error);
}

function getDocumentSetContentTypes(folderUrl,success,error)
{
      getFolder(folderUrl,
           function(folder)
           {
               var folderItem = folder.get_listItemAllFields();
               var ct = folderItem.get_contentType();
               var ctx = folder.get_context(); 
               var template = SP.DocumentSet.DocumentSetTemplate.getDocumentSetTemplate(ctx,ct);
               var ctIds = template.get_allowedContentTypes();
               ctx.load(ctIds);
               ctx.executeQueryAsync(
                 function(){
                    success(ctIds);
                 },
                 error);  
           },
           error);
}

<强>用法

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {
    SP.SOD.executeFunc('sp.documentmanagement.js', 'SP.DocumentSet.DocumentSet', function() {

        var docSetUrl = '/Documents/2013';   //<- Doc Set Url
        getDocumentSetContentTypes(docSetUrl,
           function(ctIds)
           {
               //print content type ids
               ctIds.get_data().forEach(function(ctId){
                    console.log(ctId.get_stringValue());
               });
           },
           logError);
    });   
});



function logError(sender,args){
   console.log(args.get_message());
}