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

1 Answer 1

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

5
  • What you have provided is done using CSOM, I want do it in JSOM Commented Jul 27, 2016 at 13:15
  • check this codeproject.com/Articles/560049/… Commented Jul 27, 2016 at 13:35
  • Appriciate your efforts but I want to create list using existing template from list template gallery Commented Jul 27, 2016 at 13:51
  • at this line you can specify your template SPListTemplate template = lstTemp[strTemplateName]; Commented Jul 27, 2016 at 14:20
  • The C# code is actually using server object model, not client. Commented May 3, 2017 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.