如何在sharepoint ..中使用CSOM Javascript创建页面?

时间:2019-04-15 06:44:16

标签: javascript jquery sharepoint sharepoint-online csom

我想在“页面”库中的sharepoint网站中使用csom javascript代码创建页面。 情况就像 javascript csom代码在网站的“页面”库中创建具有所有属性和页面数据内容的页面。

如果有任何示例代码,请分享。

建议表示赞赏。

谢谢。

2 个答案:

答案 0 :(得分:0)

示例Jsom代码供您参考:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="/_layouts/15/sp.runtime.js"></script>  
<script src="/_layouts/15/sp.js"></script>  
<script src="/_layouts/15/sp.publishing.js"></script>
<script type="text/javascript">  
    $(document).ready(function() {  
               createPublishingPage();
    });  

    var oWeb, clientContext, pageLayoutitem;  

    function createPublishingPage() {  
        //Get the client context,web and list object(Master Page Gallery)   
        clientContext = new SP.ClientContext.get_current();  
        oWeb = clientContext.get_web();  
        var oList = oWeb.get_lists().getByTitle('Master Page Gallery');  
        //Get the page layout by ID using which we will create a publishing page   
        pageLayoutitem = oList.getItemById(867);  
        //Load the client context and execute the batch   
        clientContext.load(oWeb);  
        clientContext.load(pageLayoutitem);  
        clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
    }  

    function QuerySuccess() {  
        //Create Publishing Page using PublishingPageInformation object   
        var newPublishingPage = SP.Publishing.PublishingWeb.getPublishingWeb(clientContext, oWeb);  
        var pageInfo = new SP.Publishing.PublishingPageInformation();  
        pageInfo.set_name("New Publishing Page.aspx");  
        pageInfo.set_pageLayoutListItem(pageLayoutitem);  
        newPage = newPublishingPage.addPublishingPage(pageInfo);
        console.log(newPage);
         var wikipage = newPage.get_listItem();
         console.log(wikipage);
           wikipage.set_item("Title","test");
           wikipage.update();
           clientContext.load(newPage);  
           clientContext.load(wikipage);
        clientContext.executeQueryAsync(SecondQuerySuccess, SecondQueryFailure);  
    }  

    function QueryFailure(sender, args) {  
        console.log('Request failed' + args.get_message());  
    }  

    function SecondQuerySuccess(sender, args) {  
        console.log("Publishing page created successfully.");  
    }  

    function SecondQueryFailure(sender, args) {  
        console.log('Request failed' + args.get_message());  
    }  
</script>

对于pageLayoutitem = oList.getItemById(867),请替换为您的环境“母版页画廊”中的真实页面布局ID,我使用“ ArticleLeft.aspx”进行测试,其ID为867:

enter image description here

enter image description here

答案 1 :(得分:0)

function createEntWikiPage(pageName,pageContent,success,error)
		{
			var templateRedirectionPageMarkup = "<%@ Page Inherits=\"Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c\" %> <%@ Reference VirtualPath=\"~TemplatePageUrl\" %> <%@ Reference VirtualPath=\"~masterurl/custom.master\" %>";
			var ctx = SP.ClientContext.get_current();
			var wikiPages = ctx.get_web().get_lists().getByTitle("Pages");
			var fileInfo = new SP.FileCreationInformation();
			fileInfo.set_url(pageName);
			var fileContent = new SP.Base64EncodedByteArray();
			for (var i = 0; i < templateRedirectionPageMarkup.length; i++) {
			fileContent.append(templateRedirectionPageMarkup.charCodeAt(i));
			}
			fileInfo.set_content(fileContent);
			fileInfo.set_overwrite(true);
			var wikiFile = wikiPages.get_rootFolder().get_files().add(fileInfo);
			var wikiPage = wikiFile.get_listItemAllFields();
			wikiPage.set_item("PublishingPageContent",pageContent);
			var siteUrl = _spPageContextInfo.siteServerRelativeUrl == '/' ? _spPageContextInfo.siteServerRelativeUrl : _spPageContextInfo.siteServerRelativeUrl + '/';
			wikiPage.set_item("PublishingPageLayout",siteUrl + "_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page");
			wikiPage.update();
			ctx.executeQueryAsync(
			function() {
				success(wikiFile);
			},error);
		}

我尝试了上面的代码。对我来说很好。