0

How to create list from list template in Host Web?

I have created SharePoint hosted app and some how I managed to copy the list template file to host web's list template gallery . Now I want create list from that list template from my app web to the host web

0

(JSOM) : add the following script to create the list.

//Add jquery version correctly   
<script type="text/javascript" src="../Scripts/jquery-1.7.1.min.js"></script>

<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/init.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>

<script type="text/ecmascript">

    function createList() {
        var clientContext = new SP.ClientContext.get_current();
        var oWebsite = clientContext.get_web();

        var listCreationInfo = new SP.ListCreationInformation();
        listCreationInfo.set_title('CustomList'); // list name
        listCreationInfo.set_description('description'); // list description
        listCreationInfo.set_templateType(SP.ListTemplateType.genericList); //list type

        oWebsite.get_lists().add(listCreationInfo);

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded),// when success
            Function.createDelegate(this, this.onQueryFailed) // when failed
            );
    }
    function onQuerySucceeded() {
        alert("List Created");
    }

    function onQueryFailed(sender, args) {
        alert("List Failed");
    }

</script>

Ref Create a SharePoint 2013 List Using JSOM (ECMAScript Client Object Model)

(CSOM) : Below is the code in which you have to send parameter :

  • strSiteURL: Site URL on which you have to create List using template.

  • strTemplateName: Template name of which you have to create list

  • strListName: Name of the list.

    public void CreateListFromTemplate(string strSiteURL,string  strTemplateName,string strListName) {
    SPSecurity.RunWithElevatedPrivileges(delegate
    
    {
    
    using (SPSite oSPsite = new SPSite(strDestinationURL))
    
    {
    
    oSPsite.AllowUnsafeUpdates = true;
    
    using (SPWeb oSPWeb = oSPsite.OpenWeb())
    
    {
    
    oSPWeb.AllowUnsafeUpdates = true;
    
    SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);
    
    SPListTemplate template = lstTemp[strTemplateName];
    
    oSPWeb.Lists.Add(txtListName.Text, "Description", template);
    
    Guid listId = oSPWeb.Lists.Add(txtListName.Text, "Description", template);
    
    SPList mylist = oSPWeb.Lists[listId];
    
    mylist.OnQuickLaunch = false;
    
    mylist.Update();
    
    oSPWeb.AllowUnsafeUpdates = false;
    
    }
    
    oSPsite.AllowUnsafeUpdates = false;
    
    }
    
    });
    
    }
    

Ref : Create List from Custom List Template Programmatically in SharePoint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.