development-environmentsharepoint-onlinesandbox-solutionsharepoint-listtemplate

how to create a list template in SharePoint online using VS 2013


I am going to create some declarative items in SPO using VS. I know how to create it through UI by I want to create programmatically. As I did some research, there are 3 ways. First is through SP hosted App(add-in) then give manage permission on host web to create some list on its parent site. Here is the article:http://www.sharepointnadeem.com/2013/12/sharepoint-2013-apps-access-data-in.html This approach is not good because App web shouldn't apply any change on its host web. Second is through Sandbox solution. Once I try to create a sandbox solution using SPO site URL, I will receive an error which said connecting to a remote site is not possible through VS. So I have to enter a local SP URL to create a sandbox solution. then I should create list template declaratively and deploy it and publish it to my SPO environment. Here is the article which explain steps:http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2012/01/10/how-to-use-visual-studio-11-to-publish-solutions-to-sharepoint-online.aspx The problem that I have with this approach is I don't have access to my managed metadata columns which are located in SPO once I am developing list template in my local development machine. Third approach is creating List or content type or template in App web which is not my case. This has been explained in this article: sharepoint-journey.com/sharepoint-list-in-sharepoint-hosted-app.html The problem here is, I want to have that template in my site (host site) not in App web site (sub site)

My question is: I want to provide some list template in my SPO environment by using VS 2013. what is the best approach for doing that? and how can I do that? Please give me an step by step instruction.

Thank you


Solution

  • You definetly want to go the App(Add-in) way. It is possible and in many cases prefereble to set up your lists on the host web and then just use the app web for a user interface, or if you don't want to utilize it at all: Just let the add-in do the heavy lifting in creating lists, columns and content types.

    The important thing here is that you specify in your code that you want the lists to be created on the Host Web and not the App Web. For this to work you need to include a permission request in your app. When installed, the app will ask the user installing it if it can have permission to the host web. This is done by setting the relevant scope in your AppManifest.xml file, and then you set it under Permission: [your scope] - [permission level] You can read more about this at this resource: https://msdn.microsoft.com/en-us/library/office/fp142383.aspx

    Then something like this code will allow you to create a list on your hostweb. Keep in mind that this list will still be available on the host web even if you remove your app.

        oApp.install.addList = function (listName) {
    
            var listCreationInfo = new SP.ListCreationInformation();
            listCreationInfo.set_title(listName);
            listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
            var myNewList = hostcontext.get_web().get_lists().add(listCreationInfo);
            var dfd = $.Deferred();
    
            context.load(myNewList);
            context.executeQueryAsync(function () {
                var listCreated = true;
                console.log("[" + listName + "]" + " added to hostweb");
                if (listCreated) dfd.resolve();
            }, oApp.onFail
            );
    
        return dfd.promise();
    };
    

    I also wrote about creating certain types of columns on your list at my blog. Feel free to check that out here: http://bayerlein.se/how-to-create-host-web-lists-with-certain-columns-in-your-sharepoint-add-in-the-nice-way/