Roshni Shah
Roshni Shah

Reputation: 309

SharePoint: Customize default NewForm.aspx and hide some fields according to given permissions

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

Answers (1)

Ronak Patel
Ronak Patel

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 develop the custom webpart for NewForm where you can display fields as per the permissions.
  • Then set this webpart in place of default NewForm.aspx, so when ever user click on NewItem link of the list then rather than default sharepoint form it will open custom webpart.
  • 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

Related Questions