1

I'm creating a web part to be added on a form (DispForm, EditForm, NewForm) that will control the visibility of the form's fields.

On the web part's tool part, I'm planning on displaying the list of fields available for them to toggle visibility on.

I'd like to do this without having to require them to select what the list is. I'd rather have it determined dynamically if possible.

So is it possible then to do this: programatically determine the list used on a form page?


More info:

  • Go to any list on a SharePoint site.
  • Click on Add New Item
  • You are displayed a dialog or a new page with the default NewForm.aspx
  • This is OOTB. However, I'm planning on adding onto this form page a new web part.
  • I want this web part to be able to determine dynamically if possible what list the item being added belongs to.
6
  • Which language are you using for your development? C#, Javascript, Powershell, etc.
    – Mike
    Feb 11, 2015 at 17:59
  • @Mike I am using C#
    – kei
    Feb 11, 2015 at 18:32
  • What then determines which list is selected?
    – jpollar
    Feb 11, 2015 at 18:34
  • There is only one list to be determined: the list of the item being displayed by the current EditForm.aspx, etc.
    – kei
    Feb 11, 2015 at 18:38
  • I wonder if you hard code a hidden asp:label with the value if that would allow you to reference it.
    – jpollar
    Feb 11, 2015 at 18:54

2 Answers 2

1

You could utilize SPContext.List Property to get the list that is associated with the SharePoint context in List forms.

Example

protected override void CreateChildControls()
{
    var currentList = SPContext.Current.List;
    foreach (var field in currentList.Fields)
    {
        //...
    }
}
1
  • 1
    Perfect! This is what I've been looking for.
    – kei
    Feb 11, 2015 at 20:49
0

Here's a workaround that I did:

Use HttpContext.Current.Request.Url to get the URL of the form.
Figure out the list name knowing the URL format for it looks like http://site/subsite/Lists/<ListName>/...
After getting the list name, getting the list is straightforward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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