简体   繁体   中英

JSLINK from c# doesn't work

I want to apply some custom render client (csr) to site column in XsltListViewWebPart with c# this is the code :

using (SPSite site = new SPSite("http://sharepoint"))
        {                               
            site.AllowUnsafeUpdates = true;

            SPField Status = web.Fields["Status"];

            Status.JSLink = "~site/_catalogs/masterpage/CustomField.js";
            Status.Update(true);
        }

I get this error Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb. Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.

SPSecurity.RunWithElevatedPrivileges doesn't work either.

When I just add list view through the browser the CSR work perfectly but when I try to add list view through c#, the CSR doesn't work.

Thank in advance for your help.

I find this link which explain a similar problem, AllowUnsafeUpdates

Anyway, I have done that trough visual studio when I define the site column, I have add a csr file in layouts folder and apply through myview.jslink ="/_Layouts/15/csr/custom_field.js";

I think also this tutorial can help to use csr to custumize site column csr tutorial

  1. we can't apply xslt file and csr file on the same view. so I have removed the xslt file and combine javascript for the csr and css for the layouts.
  2. When you use xsltlistview from webpart (c#) you have to save the javascript file in the folder layouts. and the link for this file has to be /_Layouts/15/folder/myfile.js

I hope this is can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM