How to: create Promoted Links app in C# (SharePoint 2013)
Creating most types of lists (or apps in SharePoint 2013 terminology) in C# code is pretty straightforward. You just need to choose the correct list type from the built-in enum Microsoft.Sharepoint.SPListTemplateType
. Then, you can create list by invoking appropriate Add()
method on a SPListCollection
, like in the first example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private SPList AddList(SPWeb web, SPListTemplateType templateType, string internalName, string displayName, string description) { // add list Guid listGuid = web.Lists.Add(internalName, description, templateType); // now you can modify list properties, add content types, fields etc. SPList listObj = web.Lists[listGuid]; listObj.Title = displayName; // ... listObj.Update(); return listObj; } |
However, this will not work for Promoted Link app, as it’s template is not among the built-in templates available in SPListTemplateType
enum.
Creating Promoted Links will require using overloaded method Add(string title, string description, SPTemplate template)
. Please note, that the third argument is now SPListTemplate
and not SPListTemplateType
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
private SPList AddPromotedLinks(SPWeb web, string internalName, string displayName, string description) { // add promoted links app SPListTemplate template = web.ListTemplates.OfType<SPListTemplate>() .Where(templ => templ.InternalName == "promotedlinks").FirstOrDefault(); Guid listGuid = web.Lists.Add(internalName, description, template); // now you can modify list properties SPList listObj = web.Lists[listGuid]; listObj.Title = displayName; // ... listObj.Update(); return listObj; } |
The string "promotedlinks"
is the name of the template for the list with promoted links tiles. It’s language-neutral.
Be advised that though you could get SPListTemplate
for Promoted links using bracket operator [] – web.ListTemplates["Promoted Links"]
– this relies on the display name of the feature. It means, that in localized SharePoint webs (other than with en-US locale) this code will fail, as "Promoted Links"
will not be recognized as name of any template type. Therefore, using LINQ to get the template by its internal name is much better choice.
After adding the list, you can add elements from C# code just as to any other type of list.