Reputation: 309
My requirement is to restrict users to view/add/edit fields of a list as per their given group/permission. To achieve this objective as per my understanding I have to generate custom list forms. If anyone can help me to customize NewFrom.Aspx and code in such a way that we can check current user group/permission and hide fields which should not be accessible to him.
I have derived solution after googling and have thought to apply below thing after creating new custom list form. I want to know if this approach is good enough or give any optimal solution to it.
var web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
web.Update();
var lists = web.Lists["SomeList"];
var f = lists.Fields["SomeField"];
if(){ //to check if user is in role
f.ShowInEditForm = false;
f.ShowInNewForm = false;
f.Update();
}
The same list will be used ac cross all the users so I think above solution might not be optimal.
Thanks in advance.
Upvotes: 1
Views: 1018
Reputation: 305
As SharePoint does not support Column level permissions, there are some third party tools available for column level permissions:
For Custom NewForm:
You can replace the default NewForm webpart with custom webpart with following code:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb currentWeb = (SPWeb)properties.Feature.Parent;
try
{
SPLimitedWebPartManager NewForm = currentWeb.GetLimitedWebPartManager("Lists/listname1/NewForm.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
AddWebPart(NewForm);
}
catch (Exception)
{
throw;
}
}
protected void AddWebPart(SPLimitedWebPartManager MainPage)
{
MainPage.WebParts[0].Hidden = true;
MainPage.SaveChanges(MainPage.WebParts[0]);
try
{
var linqqry = from wp in MainPage.WebParts.Cast()
where wp.GetType() == typeof(Webpart1.Webpart1)
select wp;
if (linqqry.Count() == 0)
{
//Create an instance of WPMenu Webpart and add in a Webpart zone
Webpart1.Webpart1 wpWebPart = new Webpart1.Webpart1();
MainPage.AddWebPart(wpWebPart, "Main", 0);
MainPage.SaveChanges(wpWebPart);
}
}
catch (Exception ex)
{
}
}
Upvotes: 1