如何使用JSOM自动创建以SP列表形式保存的网站?

时间:2018-01-31 16:46:14

标签: sharepoint-online sharepoint-jsom

对于其中一个客户端要求,我必须通过单击按钮自动创建Sharepoint站点。基本上会有一个SharePoint列表表单,用户将在其中输入数据作为标题,URL,并选择一个模板。点击保存后,它应该创建一个sharepoint站点。我已经能够使用HTML表单Web部件实现这一点,但现在我需要使用SharePoint OOTB列表表单并且需要做同样的事情。下面是我编写的代码,在按钮点击时创建一个sharepoint站点,代码工作正常,并根据选择创建一个站点。我在线使用SharePoint。

有关如何将此方法转换为A Sharepoint列表形式方法的任何想法?

    <script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">

function PreSaveAction(){
    CreateSubWeb();
    return true;
}

function CreateSubWeb() {
    // defualt list Title field
    var webTitle=$("input[title='Client_x0020_Name']").val();
    //custom fields URL and Template
    var url=$("input[title='Site_x0020_URL']").val();
    var template=$("input[title='Custom_x0020_Template']").val();

    // current web url
    var webUrl = _spPageContextInfo.webAbsoluteUrl;

    var clientContext = new SP.ClientContext(webUrl);
    this.oWebsite = clientContext.get_web();

    var webCreationInfo = new SP.WebCreationInformation();
    webCreationInfo.set_title(webTitle);
    webCreationInfo.set_language(1033);
    webCreationInfo.set_url(url);
    webCreationInfo.set_useSamePermissionsAsParentSite(true);

    if(template == 'Customer W Project'){       
            webCreationInfo.set_webTemplate("{2936BD84-30AD-413E-8933-2A6B7856D10F}#Template 2");
    }
    else
    {
        webCreationInfo.set_webTemplate("{ED884F01-6B10-4791-A704-FF05A047D0F3}#Template 1");   
    }
    oWebsite.get_webs().add(webCreationInfo);
    oWebsite.update();
    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

}
function onQuerySucceeded(sender, args) {
    alert('success'); 
}
function onQueryFailed(sender, args) {
     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
}  
</script>

Screenshot

1 个答案:

答案 0 :(得分:0)

以下代码供您参考,将代码添加到新表单页面的脚本编辑器Web部件中。

<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function PreSaveAction(){
    var templateName=$("select[title='Custom Template']").val();
    getTemplateName(templateName).done(function(template) {
        createSubSite(template);    
    }); 
    return true;
}
var createSubSite = function(templateName) {
    // defualt list Title field
    var webTitle=$("input[title='Title Required Field']").val();
    //custom fields URL and Template
    var url=$("input[title='Site URL']").val();

    // current web url
    var webUrl = _spPageContextInfo.webAbsoluteUrl;

    var clientContext = new SP.ClientContext(webUrl);
    this.oWebsite = clientContext.get_web();

    var webCreationInfo = new SP.WebCreationInformation();
    webCreationInfo.set_title(webTitle);
    webCreationInfo.set_language(1033);
    webCreationInfo.set_url(url);
    webCreationInfo.set_useSamePermissionsAsParentSite(true);
    webCreationInfo.set_webTemplate(templateName);
    oWebsite.get_webs().add(webCreationInfo);
    oWebsite.update();
    clientContext.executeQueryAsync(function() {
            console.log("Done");
        }, function(sender,args){ 
            console.log("failed. Message:" + args.get_message());
    });
}

var getTemplateName = function(templateName) {
    var dfd = new $.Deferred(); 
    var clientContext = SP.ClientContext.get_current();
    var templates = clientContext.get_web().getAvailableWebTemplates(1033, false);
    clientContext.load(templates);

    clientContext.executeQueryAsync(function() {
        var templateGuidName;

        for (var template in templates.get_data()) {
                if (templates.itemAt(template).get_title() === templateName) {
                    templateGuidName = templates.itemAt(template).get_name();
                    break;
                }
            }   
        dfd.resolve(templateGuidName);

     }, function() { dfd.reject(); });

    return dfd.promise();

}
</script>