protected void btnViewAvailability_Click(object sender, EventArgs e) { SPLimitedWebPartManager webPartManager = null; try { SPList list = null; SPView view = null; // get WebPartManager to identify current List/View SPWeb web = SPContext.Current.Web; webPartManager = SPContext.Current.File.GetLimitedWebPartManager(PersonalizationScope.Shared); foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webPartManager.WebParts) { XsltListViewWebPart xlvwp = wp as XsltListViewWebPart; if (xlvwp != null) { list = web.Lists[xlvwp.ListId]; view = xlvwp.View; break; } } // get list data by Filters FieldFilterOperatorsLayer fol = new FieldFilterOperatorsLayer(this.FilterConditions); CamlFiltersLayer fl = new CamlFiltersLayer(list, this.FilterQuery, fol); string myQuery = fl.GetQueryByFilters(); SPListItemCollection navigator = list.GetItems(new SPQuery() { Query = myQuery, ViewFields = "<FieldRef Name=\"ID\" />", RowLimit = view.RowLimit }); // get filter IDs string filteredIDs = string.Empty; if (navigator != null && navigator.Count > 0) { filteredIDs = String.Join(",", navigator.Cast <SPListItem>().Select(x => x.ID.ToString()).ToArray()); } string redigUrl = string.Format("{0}/{1}?FilteredIDs={2}&OrigSchiftId={3}{4}{5}", web.ServerRelativeUrl.TrimEnd('/'), this.RedirectUrl.TrimStart('/'), filteredIDs, this.Page.Request.QueryString["SchiftId"], string.IsNullOrEmpty(this.CalendarPeriod) ? "" : string.Format("&CalendarPeriod={0}", this.CalendarPeriod), FormatedCalendarDate); SPUtility.Redirect(redigUrl, SPRedirectFlags.Default, this.Context); } catch (Exception ex) { lblError.Text = ex.Message; lblError.Visible = true; } finally { if (webPartManager != null) { webPartManager.Web.Dispose(); } } }
private bool RenderListData(SPView view, TextWriter output) { if (view == null) { return(false); } FieldFilterOperatorsLayer fol = new FieldFilterOperatorsLayer(base.Web); CamlFiltersLayer fl = new CamlFiltersLayer(this.List, HttpContext.Current.Request.QueryString, fol); SPListItemCollection navigator = this.List.GetItems(new SPQuery() { Query = fl.GetQueryByFilters(), ViewFields = CamlFiltersLayer.GetViewFieldsFromView(this.View), RowLimit = this.View.RowLimit }); StringBuilder newFilterLink = new StringBuilder(); foreach (string fKey in HttpContext.Current.Request.QueryString.AllKeys.Where(x => x.StartsWith("Filter") || x.StartsWith("Sort"))) { newFilterLink.AppendFormat("{0}={1}&", fKey, HttpContext.Current.Request.QueryString[fKey]); } output.Write("{ \"Row\" : \n"); if ((navigator == null) || (navigator != null && navigator.Count == 0)) { output.Write("[]"); if (!string.IsNullOrEmpty(newFilterLink.ToString())) { output.Write(",\"FilterLink\" : \""); output.Write(JsonTextWriter.EscapeJScriptString("?" + newFilterLink.ToString())); output.Write("\""); } } else { int num8; int num9; output.Write("["); bool isFirstElem = true; string lastField = this.View.ViewFields.Cast <string>().Last(); SPFieldCollection allFields = this.List.Fields; foreach (SPListItem itm in navigator) { output.WriteLine(isFirstElem ? "{" : ",{"); // general fields output.WriteLine("\"{0}\": \"{1}\",", "ID", JsonTextWriter.EscapeJScriptString(itm.ID.ToString())); try { output.WriteLine("\"{0}\": \"{1}\",", "Title", JsonTextWriter.EscapeJScriptString(itm.Title ?? string.Empty)); } catch (Exception ex) { output.WriteLine("\"{0}\": \"{1}\",", "Title", JsonTextWriter.EscapeJScriptString(ex.Message)); } try { output.WriteLine("\"{0}\": \"{1}\",", "ContentTypeId", JsonTextWriter.EscapeJScriptString(itm["ContentTypeId"].ToString())); } catch (Exception ex) { output.WriteLine("\"{0}\": \"{1}\",", "ContentTypeId", JsonTextWriter.EscapeJScriptString(ex.Message)); } try { output.WriteLine("\"{0}\": \"{1}\",", "PermMask", JsonTextWriter.EscapeJScriptString((string)itm["PermMask"] ?? string.Empty)); } catch (Exception ex) { output.WriteLine("\"{0}\": \"{1}\",", "PermMask", JsonTextWriter.EscapeJScriptString(ex.Message)); } try { output.WriteLine("\"{0}\": \"{1}\",", "FSObjType", JsonTextWriter.EscapeJScriptString((string)itm["FSObjType"] ?? string.Empty)); } catch (Exception ex) { output.WriteLine("\"{0}\": \"{1}\",", "FSObjType", JsonTextWriter.EscapeJScriptString(ex.Message)); } try { output.WriteLine("\"{0}\": \"{1}\",", "FileLeafRef", JsonTextWriter.EscapeJScriptString((string)itm["FileLeafRef"] ?? string.Empty)); } catch (Exception ex) { output.WriteLine("\"{0}\": \"{1}\",", "FileLeafRef", JsonTextWriter.EscapeJScriptString(ex.Message)); } try { output.WriteLine("\"{0}\": \"{1}\",", "FileRef", JsonTextWriter.EscapeJScriptString((string)itm["FileRef"] ?? string.Empty)); } catch (Exception ex) { output.WriteLine("\"{0}\": \"{1}\",", "FileRef", JsonTextWriter.EscapeJScriptString(ex.Message)); } try { output.WriteLine("\"{0}\": \"{1}\",", "File_x0020_Type", JsonTextWriter.EscapeJScriptString((string)itm["File_x0020_Type"] ?? string.Empty)); } catch (Exception ex) { output.WriteLine("\"{0}\": \"{1}\",", "File_x0020_Type", JsonTextWriter.EscapeJScriptString(ex.Message)); } // view fields string valAsJson = string.Empty; foreach (string fld in this.View.ViewFields) { valAsJson = ""; try { valAsJson = allFields.GetFieldByInternalName(fld).RenderFieldValueAsJson(itm[fld]); // !!! not all SPFields override RenderFieldValueAsJson method if (valAsJson == null) { valAsJson = (itm[fld] == null) ? "\"\"" : "\"" + JsonTextWriter.EscapeJScriptString(itm[fld].ToString()) + "\""; } } catch (Exception ex) { valAsJson = "Field: " + fld + ". Error: " + ex.Message; } output.WriteLine("\"{0}\": {1}{2}", JsonTextWriter.EscapeJScriptString(fld), valAsJson, fld != lastField ? "," : string.Empty); } output.WriteLine("}"); isFirstElem = false; } output.Write("],"); // FirstRow num8 = Convert.ToInt32(this.Context.Request.QueryString["FirstRow"], CultureInfo.InvariantCulture); if (num8 < 1) { num8 = 1; } output.WriteLine("\"FirstRow\" : {0},", num8); // LastRow num9 = Math.Min((num8 + ((int)view.RowLimit)) - 1, navigator.Count); // xslWebPart.GridDataSerializer.ExplicitRowFilterCount.Value output.WriteLine("\"LastRow\" : {0}", num9); StringBuilder builder = new StringBuilder(); StringBuilder sb = new StringBuilder(); // add FilterLink if (HttpContext.Current.Request.QueryString.HasKeys()) { output.Write(",\"FilterLink\" : \""); output.Write(JsonTextWriter.EscapeJScriptString("?" + newFilterLink.ToString())); output.Write("\"\n"); } // add SortField if (HttpContext.Current.Request.QueryString.AllKeys.Contains("SortField")) { output.Write(",\"SortField\" : \""); output.Write(JsonTextWriter.EscapeJScriptString(HttpContext.Current.Request.QueryString["SortField"])); output.Write("\"\n"); } // add FilterFields var filterFieldsKeys = HttpContext.Current.Request.QueryString.AllKeys.Where(x => x.StartsWith("FilterField")); var fValAr = filterFieldsKeys.Select(x => HttpContext.Current.Request.QueryString[x]); var fVal = String.Join(";", fValAr); if (!string.IsNullOrEmpty(fVal)) { output.Write(",\"FilterFields\" : \""); output.Write(JsonTextWriter.EscapeJScriptString(";" + fVal + ";")); output.Write("\"\n"); } // add SortDir if (HttpContext.Current.Request.QueryString.AllKeys.Contains("SortDir")) { output.Write(",\"SortDir\" : \""); output.Write(JsonTextWriter.EscapeJScriptString(HttpContext.Current.Request.QueryString["SortDir"] == "Asc" ? "ascending" : "descending")); output.Write("\"\n"); } // Hierarchy output.Write(",\"ForceNoHierarchy\" : \"1\"\n"); output.Write(",\"HierarchyHasIndention\" : \"\"\n"); } output.Write("\n}"); return(true); }