Skip to content

Commit 336eb8b

Browse files
committed
additional sample on multiplewebpart issue remediation
1 parent 5edbf28 commit 336eb8b

File tree

1 file changed

+149
-1
lines changed

1 file changed

+149
-1
lines changed

docs/transform/modernize-userinterface-lists-and-libraries.md

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You can't yet transform all lists and libraries to the modern experience because
1515

1616
- Certain types of lists and libraries can be shown in modern, but are blocked due to an incompatible configuration or customization; you can take action here.
1717

18-
## List templates available in the modern user interface
18+
## List templates available in the modern user interface
1919

2020
Following are the most commonly used list template types that SharePoint can currently render in a modern user interface (as of March 2018):
2121

@@ -99,6 +99,154 @@ list.Update();
9999
context.ExecuteQuery();
100100
```
101101

102+
### Investigate and fix the "MultipleWebParts" issue
103+
104+
When the used list view page (e.g. AllItems.aspx) holds more than one web part the list will never show up using the modern user interface. You can manually check these cases by going to the list and appending `?ToolPaneView=2&pagemode=edit` to the list URL. This will bring the page in edit mode and should reveal the extra web parts and allow you to remove those. If you want to programmatically do the same then below code snippet is a good starting basis. This snippet depends on the PnP sites core library which you can install to your Visual Studio project via the [SharePointPnPCoreOnline nuget package](https://www.nuget.org/packages/SharePointPnPCoreOnline/).
105+
106+
```csharp
107+
using Microsoft.SharePoint.Client;
108+
using Microsoft.SharePoint.Client.WebParts;
109+
using OfficeDevPnP.Core;
110+
using System;
111+
using System.Collections.Generic;
112+
using System.Linq;
113+
using System.Security;
114+
115+
namespace MultipleWebPartFixer
116+
{
117+
class Program
118+
{
119+
static void Main(string[] args)
120+
{
121+
string siteUrl = "https://contoso.sharepoint.com/sites/demo";
122+
string userName = "[email protected]";
123+
AuthenticationManager am = new AuthenticationManager();
124+
using (var cc = am.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, GetSecureString("Password")))
125+
{
126+
// Grab the list
127+
var list = cc.Web.Lists.GetByTitle("listtofix");
128+
list.EnsureProperties(l => l.RootFolder, l => l.Id);
129+
130+
bool isNoScriptSite = cc.Web.IsNoScriptSite();
131+
132+
if (isNoScriptSite)
133+
{
134+
throw new Exception("You don't have the needed permissions to apply this fix!");
135+
}
136+
137+
// get the current (allitems) form
138+
var files = list.RootFolder.FindFiles("allitems.aspx");
139+
var allItemsForm = files.FirstOrDefault();
140+
if (allItemsForm != null)
141+
{
142+
// Load web part manager and web parts
143+
var limitedWPManager = allItemsForm.GetLimitedWebPartManager(PersonalizationScope.Shared);
144+
cc.Load(limitedWPManager);
145+
146+
// Load the web parts on the page
147+
IEnumerable<WebPartDefinition> webParts = cc.LoadQuery(limitedWPManager.WebParts.IncludeWithDefaultProperties(wp => wp.Id, wp => wp.ZoneId, wp => wp.WebPart.ExportMode, wp => wp.WebPart.Title, wp => wp.WebPart.ZoneIndex, wp => wp.WebPart.IsClosed, wp => wp.WebPart.Hidden, wp => wp.WebPart.Properties));
148+
cc.ExecuteQueryRetry();
149+
150+
List<WebPartDefinition> webPartsToDelete = new List<WebPartDefinition>();
151+
if (webParts.Count() > 1)
152+
{
153+
// List all except the XsltListView web part(s)
154+
foreach (var webPart in webParts)
155+
{
156+
if (GetTypeFromProperties(webPart.WebPart.Properties) != "XsltListView")
157+
{
158+
webPartsToDelete.Add(webPart);
159+
}
160+
}
161+
162+
if (webPartsToDelete.Count == webParts.Count() - 1)
163+
{
164+
foreach(var webPart in webPartsToDelete)
165+
{
166+
webPart.DeleteWebPart();
167+
}
168+
cc.ExecuteQueryRetry();
169+
Console.WriteLine("List fixed!");
170+
}
171+
else
172+
{
173+
// Special case...investigation needed. Go to list and append ?ToolPaneView=2&pagemode=edit to the list url to check the page
174+
Console.WriteLine("Go to list and append ?ToolPaneView=2&pagemode=edit to the list url to check this page");
175+
}
176+
}
177+
}
178+
}
179+
180+
Console.WriteLine("Press enter to continue...");
181+
Console.ReadLine();
182+
}
183+
184+
public static string GetTypeFromProperties(PropertyValues properties)
185+
{
186+
// Check for XSLTListView web part
187+
string[] xsltWebPart = new string[] { "ListUrl", "ListId", "Xsl", "JSLink", "ShowTimelineIfAvailable" };
188+
if (CheckWebPartProperties(xsltWebPart, properties))
189+
{
190+
return "XsltListView";
191+
}
192+
193+
return "";
194+
}
195+
private static bool CheckWebPartProperties(string[] propertiesToCheck, PropertyValues properties)
196+
{
197+
bool isWebPart = true;
198+
foreach (var wpProp in propertiesToCheck)
199+
{
200+
if (!properties.FieldValues.ContainsKey(wpProp))
201+
{
202+
isWebPart = false;
203+
break;
204+
}
205+
}
206+
207+
return isWebPart;
208+
}
209+
210+
private static SecureString GetSecureString(string label)
211+
{
212+
SecureString sStrPwd = new SecureString();
213+
try
214+
{
215+
Console.Write(String.Format("{0}: ", label));
216+
217+
for (ConsoleKeyInfo keyInfo = Console.ReadKey(true); keyInfo.Key != ConsoleKey.Enter; keyInfo = Console.ReadKey(true))
218+
{
219+
if (keyInfo.Key == ConsoleKey.Backspace)
220+
{
221+
if (sStrPwd.Length > 0)
222+
{
223+
sStrPwd.RemoveAt(sStrPwd.Length - 1);
224+
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
225+
Console.Write(" ");
226+
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
227+
}
228+
}
229+
else if (keyInfo.Key != ConsoleKey.Enter)
230+
{
231+
Console.Write("*");
232+
sStrPwd.AppendChar(keyInfo.KeyChar);
233+
}
234+
235+
}
236+
Console.WriteLine("");
237+
}
238+
catch (Exception e)
239+
{
240+
sStrPwd = null;
241+
Console.WriteLine(e.Message);
242+
}
243+
244+
return sStrPwd;
245+
}
246+
}
247+
}
248+
```
249+
102250
## See also
103251

104252
- [Modernize the user interface](modernize-userinterface.md)

0 commit comments

Comments
 (0)