Pergunta

Here is the problem: using SPD is easy to change properties to not allow users to close / minimize webparts from a wp zone:

webpartzone edit properties http://img34.imageshack.us/img34/3389/webpartzoneedit.png

Now how can we do that in code ? I have more then 30 aspx webpart pages on a site, I want to be able to do that so no one can change webparts properties (close, delete them etc.)

The only thing I did find is this link: spwebpartzonetools.

Reading more in SDK at: wss 3.0 sdk, at the second Important paragraph it says:

Although you can add Web Parts to the Display, Edit, and New forms for list items (DispForm.aspx, EditForm.aspx, and NewForm.aspx), doing so is not recommended or supported in Windows SharePoint Services. Adding Web Parts to list view pages (AllItems.aspx) is supported.

So that's my mistake that I want to fix :). I did break that rule. My problem can be fixed in SPD but I don't know if that can be fixed in code.

The mistake was "inspired" from one of the fabulous 40 Templates : BudgetingTrackingMultipleProjects.wsp. There on Projects list, on DispForm.aspx, Microsoft add more webparts to create a master-detail form.

Foi útil?

Solução

I've solved my problem like this: if you open the DispForm.aspx in SPD and modify web part zone properties then in the souce code (aspx) we can see this line:

 <WebPartPages:WebPartZone runat="server" FrameType="None" ID="Main" Title="loc:Main" allowlayoutchange="false" allowpersonalization="false" allowcustomization="false">

So then I've create a simple console app, that visits each list, get the url of its DispForm.aspx and open it like a regular text file. So I'm not using any API or smth, just SPWeb.GetFile(String) - to get the SPFile and SPWeb.GetFileAsString(url) to get the aspx content as string. Then I'm injecting the attributes (allowlayoutchange / allowpersonalization / allowcustomization) in the line presented above using just string search and replace methods.

Then save the content back to aspx file using SPFile.SaveBinary(). Thats it, next time DispForm.aspx is requested the contrib. user can't do a thing with webparts from there. So that is my method to change web part zone properties. I'm so looking forward to see other solutions.

Outras dicas

You should be able to set these properties after provisioning the web parts (that is if you havent done as Chris say and defined it in the manifest of the web part) by iterating the web parts in the SPLimitedWebPartManager (beware of leak) on each page and set the AllowClose, AllowHide, AllowMinimize etc properties.

hth Anders Rask

You could also set this to the WebPartZone control (in code or in aspx). Use the LockLayout property, for instance like this:

<WebPartPages:WebPartZone LockLayout="true" runat="server"...>...</...>

/WW

Do you need to do it in code? If your requirement is simply to stop users making these changes in your web parts, this is typically dealt with by provisioning the web part with properties which specify this behaviour e.g.:

   <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
     <metaData>
       <type name="xxxxx.SharePoint.Common.WebParts.SiteDirectoryWebPartWrapper,
 xxxxxx.SharePoint.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxx" />
       <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
     </metaData>
     <data>
       <properties>
         <property name="AllowZoneChange" type="bool">False</property>
         <property name="AllowHide" type="bool">False</property>
         <property name="AllowMinimize" type="bool">False</property>
         <property name="AllowClose" type="bool">False</property>
         <property name="AllowEdit" type="bool">True</property>
         <property name="AllowConnect" type="bool">False</property>
         <property name="ExportMode" type="exportmode">All</property>
         <property name="Hidden" type="bool">False</property>
         <property name="TitleUrl" type="string" />
         <property name="Description" type="string">Displays a list of sites
 for the current area (e.g. 'teams') in a treeview. </property>
         <property name="Title" type="string">xxxxx Site Directory
 Web Part</property>
       </properties>
     </data>   
    </webPart> 
</webParts>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top