How to access the webpartmanager of a list?
-
09-12-2019 - |
Question
I would like to programmatically add a CEWP to a list (to perform operations on my list forms), it's very easy to do it through the SP interface but I can't manage to reach the webpartmanager of my list with the code...
Thank you by advance
Solution
Your list does not contain a webpart manager. Your pages contain a webpart manager. Luckily the disp/edit forms are pages in your "list folder".
That means you should be able to open the desired list form in a webpart manager:
var editFormUrl = listUrl + "/Forms/EditForm.aspx";
var wpm = web.GetLimitedWebPartManager( editFormUrl, PersonalizationScope.Shared );
wpm.AddWebPart( new YourCustomWebPart(), "Main", 0 );
That should give you a webpart manager for the Edit Form in a document library and add a webpart named YourCustomWebPart
to it (regular lists lack the /Forms/ part of the url, so for those lists, just remove it).
FYI: List forms contains only one webpart zone named "Main", and it already has a webpart of type ListFormWebPart in it. This is the webpart that shows you the list item data.
I have not tested manipulating these forms this way, so I do not know if this actually works, but this is how you work with the SPLimitedWebPartManager anyway :).