从WebService调用Sitefinity ListItems方法时抛出异常
Let's walk through the most common reasons why calling App.WorkWith().ListItems() throws exceptions in a WebService context, plus concrete fixes you can implement right away:
1. Missing Sitefinity Context Initialization
When running code outside Sitefinity's standard request pipeline (like in a custom WebService), the platform's context often isn't properly initialized. This is the #1 culprit for these kinds of errors.
Fix: Add context initialization at the start of your WebService method:
// Basic initialization for single-site setups Telerik.Sitefinity.Abstractions.SiteInitializer.Initialize(); // For multi-site environments, target a specific site var targetSite = Telerik.Sitefinity.Multisite.SiteManager.GetManager().GetSite("your-site-url-name"); Telerik.Sitefinity.Abstractions.SiteInitializer.Initialize(targetSite);
2. Insufficient Permissions
WebService requests run under a specific identity, which may lack permissions to access Live ListItems content.
Fix:
Either:
- Configure your WebService to run under a user account with Content View permissions for the target list.
Or, temporarily elevate permissions using Sitefinity's security wrapper:
Telerik.Sitefinity.Security.SecurityManager.RunAs(() => { // Your ListItems query logic goes here }, Telerik.Sitefinity.Security.UserManager.GetManager().GetAdminUser());
3. Invalid List Reference
Your query uses o.Parent.UrlName == "listado-direcciones-formularios-venta"—if the list's UrlName is misspelled, case-mismatched, or the list no longer exists, this will fail.
Fix:
First, verify the list's exact UrlName in the Sitefinity backend (go to Content > Lists and check the list's properties). Then, make your query more reliable by fetching the list first:
var targetList = App.WorkWith().Lists() .Where(l => l.UrlName == "listado-direcciones-formularios-venta") .GetFirstOrDefault(); if (targetList == null) { throw new InvalidOperationException("Target list 'listado-direcciones-formularios-venta' not found"); } // Now use the list's ID for the query var calles = App.WorkWith().ListItems() .Where(o => o.Parent.Id == targetList.Id && o.Status == ContentLifecycleStatus.Live) .Get() .OrderBy(o => o.Ordinal);
4. Lazy Loading Issues
Accessing o.Parent directly in your Where clause can trigger lazy-loading errors, as Sitefinity's ORM may not be able to resolve the parent list in a non-pipeline context.
Fix: Eagerly load the parent list with the Include method:
var calles = App.WorkWith().ListItems() .Include(o => o.Parent) .Where(o => o.Parent.UrlName == "listado-direcciones-formularios-venta" && o.Status == ContentLifecycleStatus.Live) .Get() .OrderBy(o => o.Ordinal);
5. WebService Hosting Constraints
If your WebService is a separate project (not hosted within the Sitefinity web app), it may lack access to Sitefinity's configuration and core assemblies.
Fix:
- Deploy your WebService within the Sitefinity web application to share the same app domain.
- If it must be a separate project, ensure all required Sitefinity assemblies are referenced, and your web.config includes Sitefinity's configuration sections.
Final Encapsulated WebService Method Example
Here’s how your method should look with these fixes applied:
[WebMethod] public List<SimpleTO> GetDirecciones() { var direcciones = new List<SimpleTO>(); // Initialize Sitefinity context Telerik.Sitefinity.Abstractions.SiteInitializer.Initialize(); try { // Locate the target list var targetList = App.WorkWith().Lists() .Where(l => l.UrlName == "listado-direcciones-formularios-venta") .GetFirstOrDefault(); if (targetList == null) { throw new InvalidOperationException("Listado de direcciones no encontrado"); } // Run with elevated permissions to access Live content Telerik.Sitefinity.Security.SecurityManager.RunAs(() => { var calles = App.WorkWith().ListItems() .Where(o => o.Parent.Id == targetList.Id && o.Status == ContentLifecycleStatus.Live) .Get() .OrderBy(o => o.Ordinal); foreach (var calle in calles) { var item = new SimpleTO(); item.id = calle.Title.ToUpper(); // Populate additional SimpleTO properties here direcciones.Add(item); } }, Telerik.Sitefinity.Security.UserManager.GetManager().GetAdminUser()); } catch (Exception ex) { // Log the error for debugging (use Sitefinity's built-in logger) Telerik.Sitefinity.Services.SystemManager.GetLogger().Error("Error retrieving direcciones", ex); throw; // Or return a user-friendly error response } return direcciones; }
Adjust the code to match your SimpleTO structure and any multi-site configurations you have in place.
内容的提问来源于stack exchange,提问作者Mauricio Gracia Gutierrez




