1

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

1 Answers1

2

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/

Kodz
  • 380
  • 2
  • 13
  • Hi Ludvig, Thanks for detail answer. You want to say the best solution is App not Sandbox. Just to confirm, for my scenario I should create an app which will create a List temp or Content Type on Host Web then app has finished its duty. since we don't have access to SOM in SP hosted App, I should use CSOM for list creation. Is that right? I read an article in the web which says, although MS push App solution and Sandbox has deprecated, but there are some scenario (such as list and CT creation) in SPO which the only solution is Sandbox. Can we write a sandbox solutions for this scenario? – ehsan hakimi Nov 04 '15 at 23:25
  • This article answer my question in SP 2010. https://msdn.microsoft.com/en-us/library/office/gg454741(v=office.14).aspx I assume this is the same for SP 2013. If I want to implement it through Sandbox on my local machine then upload WSP to SPO, I am not sure about one thing. I have a managed metadata field in SPO and I want my List template or Content type include that MMD field. Since I am developing on my local machine, I don't have access to that field. How can I include that field in my Content Type? Is this work if I just copy GUID of that MMD from SPO and paste it in my sandbox solution? – ehsan hakimi Nov 05 '15 at 01:08
  • We can absolutly use a sandbox solution for your case. I would however solve this problem with a App(Add-in) but its basiclly up the the developer. Both Apps(Add-ins) and sandbox (no-code solutions) aren't depricated. – Kodz Nov 05 '15 at 07:47
  • 1
    When it comes to your MMD field, which is a whole other question :), it can be a bit more tricky since MMD's and content types have a tendency to have their own way sometimes. You can solve this by either create your CT on your SPO and then reference it in your sandbox solution. This should bring in the MMD field correctly. You could also create a MMD field in the sandbox and then point out the term-set in your SPO environment. Neither is a perfect solution but it may solve the issue. The way you explained with the GUID might also work, i'm not sure however. – Kodz Nov 05 '15 at 07:51
  • Please set the question as answered if the reply solved your problem. – Kodz Nov 06 '15 at 07:18
  • Thanks Ludvig, it work in SPO by App. As we define MMD in Portal Admin manually, I have to use that one. MMD work fine when I use CT. Thank you. – ehsan hakimi Nov 06 '15 at 23:46