GetWebTemplate web method does not return & AddList web method does not recognize my custom list templates!

When you use the “GetWebTemplate” web method provided through webs.asmx web service, you’ll notice that the custom list templates you might have created is not available in the collection returned. This is because the OM call that happens behind-the-wires, calls the SPWeb.ListTemplates collection object. This object does not return the custom list templates (simply called “stp” files).

Similar is the case with “AddList” web method available through lists.asmx web service. The “TemplateID” attribute refers to the type attribute, which indicates what type of list we are trying to create. Refer Type in List Template Element (List Templates). Since list templates have a template id already “injected”, it would not be possible for us to distinguish between the built-in list types and custom list templates.

I think this would not be the case for custom list definition though! However, I have not tested that. But going by the logic, it should be so.

Well, is there a way to get custom list templates and be able to create a list based on custom list templates? Yes – you can write a custom web service to do this. Following are the code snippets you can have a quick sneak peek into.

The custom web methods:

[WebMethod]

            public string[] FetchCustomListTemplates(string siteUrl)

            {

                  string[] result=null;

                  using(SPSite site = new SPSite(siteUrl))

                  {

                        using(SPWeb web = site.OpenWeb())

                        {

                              site.AllowUnsafeUpdates=true;

                              web.AllowUnsafeUpdates=true;

                              int i=0;

                              SPListTemplateCollection sListTemplates = site.GetCustomListTemplates(web);

                              result = new string[sListTemplates.Count];

                              foreach(SPListTemplate sListTemplate in sListTemplates)

                              {

                                    result[i] = sListTemplate.Name;

                                    i++;

                              }

                        }

                  }

                  return result;

            }

            [WebMethod]

            public Guid CreateNewList(string siteUrl, string listTemplateName,string newListName,string newListDescription)

            {

                  Guid newListGuid;

                  using(SPSite site = new SPSite(siteUrl))

                  {

                        using(SPWeb web = site.OpenWeb())

                        {

                              site.AllowUnsafeUpdates=true;

                              web.AllowUnsafeUpdates=true;

                              SPListCollection lists = web.Lists;

                              SPListTemplate lt = site.GetCustomListTemplates(web)[listTemplateName];

                              newListGuid = lists.Add(newListName,newListDescription,lt);

                        }

                  }

                  return newListGuid;

            }

I’ve also attached the sample I built for one of my customer and below are the steps to install it.

1. Change CustomLists.zip.txt to CustomLists.zip and extract the files/folders within it.

2. CustomListsSrc is the custom web service source that you can refer to.

3. Copy the files you’ll find under CustomLists\60\ISAPI folder to C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\ISAPI on your SharePoint server.

4. You will have a spdisco.aspx file already in the above path. Please back it up and copy this one in its place.

5. Copy the assembly file under CustomLists\60\ISAPI\BIN folder to C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\ISAPI\BIN on your SharePoint server.

6. Upload Sridhar Link.stp list template file to your SharePoint site, which you can use to test the custom web service (BTW, there’s not hard-coding, so you can test this with any list template you might already have).

7. Goto C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\isapi path on your SharePoint server and open the web.config file you’ll find there. Back up the OOTB web.config file and do modifications provided in point 8 below.

8. Comment out <remove name=”HttpPostLocalhost” /> so that your web.config file looks like:

<configuration>

   <system.web>

      <webServices>

         <protocols>

            <remove name="HttpGet" />

            <remove name="HttpPost" />

<!--

       <remove name="HttpPostLocalhost" />

-->

        <add name="Documentation" />

         </protocols>

      </webServices>

      <customErrors mode="On"/>

      <trust level="Full" originUrl="" />

   </system.web>

</configuration>

9. Issue an IISRESET and browse to the web service through https://yoursharepointsiteurl/_vti_bin/customlists.asmx URL.

10. At this point, you can see two web methods: FetchCustomListTemplates & CreateNewList.

11. FetchCustomListTemplates  “only” return custom list template names (in string[] array) and accepts the site URL as input.

12. CreateNewList returns the GUID of the list when it gets created successfully and accepts: site URL, Template Name (as seen from the UI), List Name & List Description. E.g., for Template Name would be “Sridhar Link”.

13. You can test by providing values interactively via UI or build a custom application and call this web service

Many customers would want to get their tasks achieved remotely (i.e., by not running code directly on server, which is a requirement if you use SharePoint OM). An effective way of achieving this is by developing custom web services. Many programming tasks that can be performed via OM can also be done through a custom web service and really helps if customized SharePoint solutions need to run against SharePoint.

For complete understanding on developing and deploying custom web services, refer to Creating a Web Service for Remote Check-In and Check-Out. This walk-through is awesome as it provides detailed steps on deploying web service, which at time becomes very tricky. The walk-through provides code sample on how to perform a check-in/check-out functionality using custom web service. And the same approach can be used for implementing the above sample too.

Tip: Please don’t copy/paste elements you need to add to disco/wsdl files as they might inject junk characters, which will cause issue and would be hard to trace out and fix.

CustomLists.zip