private void CreateNewsListView(SPWeb web, SPList list) { // create new view with custom webpart SPViewCollection allviews = list.Views; string viewName = Constants.NEWS_LISTPAGE; System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection(); var view = allviews.Add(viewName, viewFields, string.Empty, 1, true, true); WebPartHelper.HideXsltListViewWebParts(web, view.Url); WebPartHelper.ProvisionWebpart(web, new WebpartPageDefinitionCollection() { new WebpartPageDefinition() { PageUrl = view.Url, Title = list.Title, Webparts = new System.Collections.Generic.List <WebpartDefinition>() { new DefaultWP() { Index = 0, ZoneId = "Main", WebpartName = "NewsListView.webpart" } } } }); WebPartHelper.MoveWebPart(web, view.Url, "NewsListView.webpart", "Main", 0); view.Update(); //list.Update(); }
private void CreateView(string viewName, string listTitle, string[] viewFields, SPWeb web) { SPList list = web.Lists.TryGetList(listTitle); if (list != null) { StringCollection viewFieldNames = new StringCollection(); foreach (var viewField in viewFields) { viewFieldNames.Add(viewField); } SPViewCollection views = list.Views; views.Add(viewName, viewFieldNames, null, 100, true, true); list.Update(); web.Update(); } }
private void CreateTheView(SPViewCollection theViews, string viewName) { try {//if the view exist do nothing SPView theView = theViews[viewName]; } catch {//if not, create it System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection(); viewFields.Add("EventDate"); viewFields.Add("EndDate"); viewFields.Add("Title"); //viewFields.Add("_x00c1_rea_x0020_reserva"); viewFields.Add("fRecurrence"); string query = string.Format( "<Where>" + "<And>" + "<DateRangesOverlap>" + "<FieldRef Name='EventDate' />" + "<FieldRef Name='EndDate' />" + "<FieldRef Name='RecurrenceID' />" + "<Value Type='DateTime'><Month /></Value>" + "</DateRangesOverlap>" + "<Eq><FieldRef Name='Sala_x0020_reserva' /><Value Type='Text'>{0}</Value></Eq>" + "</And>" + "</Where>", viewName); SPView theView = theViews.Add(viewName, viewFields, query, 0, false, false, SPViewCollection.SPViewType.Calendar | SPViewCollection.SPViewType.Recurrence, false); theView.ViewData = "<FieldRef Name='Title' Type='CalendarMonthTitle' />" + "<FieldRef Name='Title' Type='CalendarWeekTitle' />" + "<FieldRef Name='_x00c1_rea_x0020_reserva' Type='CalendarWeekLocation' />" + "<FieldRef Name='Title' Type='CalendarDayTitle' />" + "<FieldRef Name='_x00c1_rea_x0020_reserva' Type='CalendarDayLocation' />"; theView.Scope = SPViewScope.Recursive; theView.MobileView = true; theView.Update(); } }
public static void CreateDefaultListView(SPList list) { SPViewCollection views = list.Views; string viewName = Language.SMUListViewName; System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection(); viewFields.Add("ID"); viewFields.Add("Type"); viewFields.Add("smReceivers"); viewFields.Add("Author"); viewFields.Add("Created"); StringBuilder query = new StringBuilder(); query.Append("<Where></Where><OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>"); views.Add(viewName, viewFields, query.ToString(), 100, true, true); SPView allItems = views.Cast <SPView>().FirstOrDefault(p => p.Title == "All Items"); if (allItems != null) { views.Delete(allItems.ID); } }
/// <summary> /// Ensures the view. /// </summary> /// <param name="viewCollection">The view collection.</param> /// <param name="viewInfo">The view information.</param> /// <returns>The ensured view.</returns> public SPView EnsureView(SPViewCollection viewCollection, ViewInfo viewInfo) { var views = viewCollection.Cast<SPView>(); var ensuredView = views.SingleOrDefault(view => view.Title.Equals(viewInfo.Name, StringComparison.OrdinalIgnoreCase)); // If view collection doesn't already contain a view with the same name, create it if (ensuredView == null) { // Create view fields string collection var viewFields = new StringCollection(); viewFields.AddRange(viewInfo.ViewFields); if (!string.IsNullOrEmpty(viewInfo.ProjectedFields) || !string.IsNullOrEmpty(viewInfo.Joins)) { ensuredView = viewCollection.Add( viewInfo.Name, viewFields, viewInfo.Query, viewInfo.Joins, viewInfo.ProjectedFields, viewInfo.RowLimit, viewInfo.IsPaged, viewInfo.IsDefaultView, viewInfo.ViewType, viewInfo.IsPersonalView); } else { ensuredView = viewCollection.Add( viewInfo.Name, viewFields, viewInfo.Query, viewInfo.RowLimit, viewInfo.IsPaged, viewInfo.IsDefaultView, viewInfo.ViewType, viewInfo.IsPersonalView); } viewCollection.List.Update(); this.logger.Info( "View '{0}' has been successfully created in list '{1}'.", viewInfo.Name, viewCollection.List.Title); } else { this.logger.Warn( "View '{0}' has already been created in list '{1}'. Updating the view.", viewInfo.Name, viewCollection.List.Title); // Update the existing view ensuredView.ViewFields.DeleteAll(); viewInfo.ViewFields.ToList().ForEach(vf => ensuredView.ViewFields.Add(vf)); ensuredView.Query = viewInfo.Query; ensuredView.Joins = viewInfo.Joins; ensuredView.ProjectedFields = viewInfo.ProjectedFields; ensuredView.RowLimit = viewInfo.RowLimit; ensuredView.Paged = viewInfo.IsPaged; ensuredView.DefaultView = viewInfo.IsDefaultView; } if (!string.IsNullOrEmpty(viewInfo.ViewData)) { ensuredView.ViewData = viewInfo.ViewData; } ensuredView.Update(); return ensuredView; }