0

I have an event receiver which get fired when and item is create. the event receiver will create a new sub-site based on a site template. here is part of the code which is responsible for creating the subsite:-

SPWeb newSite = spCurrentSite.Webs.Add(curItemID, curItemSiteName, "created automatically after adding a new project item", Convert.ToUInt16(1033), webTemplate, false, false);
newSite.Navigation.UseShared = true;
string masterPage = "Custom.master";
var masterUri = new Uri(rootWeb.Url + "/_catalogs/masterpage/" + masterPage);
newSite.BreakRoleInheritance(true, true);
SPGroup group = newSite.SiteGroups["Project Requestors"];
newSite.RoleAssignments.RemoveById(group.ID);
//newSite.Permissions.Remove();
newSite.MasterUrl = masterUri.AbsolutePath;

newSite.CustomMasterUrl = masterUri.AbsolutePath;

newSite.Update();

newSite.Dispose();

now inside the new subsite there is a list named "Business Risks". and inside the "AllItems" view webpart, i want to assign a JSLINK to the webpart. now using the UI i can simply follow these 4 steps:-

  1. go to the Allitems view page.
  2. edit the page.
  3. edit the webpart.
  4. then under the WebPart Miscellaneous tab to specify the location of the JSLINK.

so can i do these steps inside the event receiver ?

2 Answers 2

2

Try the below code after your subsite is created:

Option 1: - easier, simpler

SPList brlist = web.Lists.TryGetList("Business Risks");
SPView brlistDefaultView = brlist.DefaultView;
brlistDefaultView.JSLink = "/path-to-jslink/jslink.js";
brlistDefaultView.Update()
web.Update();

Option 2: - a bit more complex

SPList list = web.Lists.TryGetList("Business Risks");
if(list!=null)
{
    var file = list.ParentWeb.GetFile(list.Forms[PAGETYPE.PAGE_DEFAULTVIEW].Url);
    // var file = list.ParentWeb.GetFile("/path-to-allitems.aspx");
    file.CheckOut();

    using (var manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
    {
        var webPart = manager.WebParts.OfType<ListFormWebPart>().FirstOrDefault();
        //var webPart = manager.WebParts[0];
        if (webPart != null)
        {
            webPart.JSLink = "/path-to-jslink/customjslink.js"; //change as per your file name
            manager.SaveChanges(webPart);
        }
    }

    file.CheckIn("Added JSLink to the Form");
}

Here, web is the SPWeb object of the created subsite.

3
  • will try you appraoch.. but based on the other reply by moe.. he mentioned that this can not be achieved inside the event receiver...
    – John John
    Commented May 23, 2017 at 12:43
  • now your approach will work well.. but i find that adding the jslink reference inside the original site and save the site as template will preserve the jslink .. so no need to assign the jslink using code.. i though that saving a site as template will not preserve the JSLINK settings for the web part,, since i am saving the site as templete without content... but this is not the case...
    – John John
    Commented May 23, 2017 at 16:33
  • ok in that case, this code is not needed in event receiver. Commented May 24, 2017 at 5:36
2

When you create a site based on a template which includes your "Business Risks"-list, this process includes a hidden step where a List View Web Part associated to the "Business Risks"-list is created. This web part, which isn't directly available in the web part gallery, is therefore a unique instance with unique identifying properties. So now we have a List and a List View Web Part, which are associated, but two different things.

When you want to display your list using AllItems-view, this is done using the auto-created List View Web Part, which in 2013-version is masked as adding an app to a page. So when your template includes a page with an app of your list, the page actually has the List View Web Part of your list.

This is all coming down to the part where you don't actually have access to your to-be-created List View Web Part until it actually is created. Hence every time you create a site based on a template, you need to do the described action steps in order to add the JSLINK to your web part.

Edit: There has been a similar question earlier: Keep JS Link when adding a list as a webpart. This includes a link to Ms's documentation which describes List View Web Parts in more detail.

8
  • so are you saying that i can not do this inside my event receiver ??
    – John John
    Commented May 23, 2017 at 12:39
  • @johnG correct.
    – moe
    Commented May 23, 2017 at 12:40
  • another point which came to my mind... now if i assign the JSLINK to my webpart, and i re-save the original site as template (without content).. and i create a new subsite based on the new template.in this case will the new subsite's webpart got the link to the JSLINK ..??
    – John John
    Commented May 23, 2017 at 12:41
  • second question what about Gautam Sheth answer... will it works?
    – John John
    Commented May 23, 2017 at 12:42
  • @johnG I have a sophisticated guess that even including the JSLINK in your site template's web part will not be carried to a new, template-based site. IMO it falls to the same scenario I've described. Though you can give it a shot.
    – moe
    Commented May 23, 2017 at 12:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.