private string CreateCSOMQuery()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<View>");
            sb.Append("<Query>");
            sb.Append(Caml);
            if (!String.IsNullOrEmpty(OrderByFields))
            {
                sb.Append("<OrderBy>");
                sb.Append(OrderByFields);
                sb.Append("</OrderBy>");
            }
            sb.Append("</Query>");

            if (RowLimit != null && RowLimit > 0)
            {
                sb.Append("<RowLimit>" + RowLimit.ToString() + "</RowLimit>");
            }

            if (!String.IsNullOrEmpty(ViewFields))
            {
                sb.Append("<ViewFields>" + ViewFields.ToString() + "</ViewFields>");
            }
            sb.Append("</View>");


            return(sb.ToString());
        }
Exemplo n.º 2
0
        public override XElement ToXElement()
        {
            var el = base.ToXElement();

            if (Scope != ViewScope.DefaultValue)
            {
                el.Add(new XAttribute("Scope", Enum.GetName(typeof(ViewScope), Scope)));
            }
            var queryElement = Query.ToXElement();

            if (queryElement != null)
            {
                el.Add(queryElement);
            }
            if ((ViewFields != null ? ViewFields.FieldRefs : null) != null && ViewFields.FieldRefs.Any())
            {
                el.Add(ViewFields.ToXElement());
            }
            if (Joins != null && Joins.Any())
            {
                el.Add(Joins.ToXElement());
            }
            if (ProjectedFields != null && ProjectedFields.Any())
            {
                el.Add(ProjectedFields.ToXElement());
            }
            if (RowLimit != null && RowLimit.Limit > 0)
            {
                el.Add(RowLimit.ToXElement());
            }
            return(el);
        }
Exemplo n.º 3
0
        public void AddFieldRefReturnsAViewFieldsTagWithAFieldRef()
        {
            var name = Fixture.Create <string>();
            var sut  = new ViewFields();

            sut.AddFieldRef(name);
            sut.ToString().ShouldBe(string.Format(@"<ViewFields><FieldRef Name=""{0}"" /></ViewFields>", name));
        }
Exemplo n.º 4
0
        public void AddFieldRefWithALambdaReturnsAViewFieldsTagWithAFieldRefAndCallsTheLamda()
        {
            var name     = Fixture.Create <string>();
            var attrName = Fixture.Create <string>();
            var attrVal  = Fixture.Create <string>();
            var sut      = new ViewFields();

            sut.AddFieldRef(name, r => r.AddAttribute(attrName, attrVal));
            sut.ToString().ShouldBe(string.Format(@"<ViewFields><FieldRef Name=""{0}"" {1}=""{2}"" /></ViewFields>", name, attrName, attrVal));
        }
    /// <summary>
    /// Gets View based on <see cref="Query"/>, <see cref="Scope"/>, <see cref="RowLimit"/> and <see cref="ViewFields"/> properties.
    /// </summary>
    /// <returns>View.</returns>
    private SharePointView GetView()
    {
        SharePointView view = new SharePointView
        {
            QueryInnerXml = Query,
            Scope         = Scope,
            RowLimit      = RowLimit
        };

        view.ViewFields = (ViewFields != null) ? ViewFields.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList() : null;

        return(view);
    }
        private void AppendViewFields(StringBuilder queryXml)
        {
            if (ViewFields == null || ViewFields.Length <= 0)
            {
                return;
            }

            queryXml.Append("<ViewFields>");
            foreach (var fieldName in ViewFields.Union(defaultViewFields))
            {
                queryXml.AppendFormat("<FieldRef Name='{0}' />", fieldName);
            }
            queryXml.Append("</ViewFields>");
        }
    /// <summary>
    /// Queries SharePoint for list items.
    /// </summary>
    /// <param name="listService">List service proxy class instance</param>
    /// <param name="listName">List name</param>
    /// <returns>CAML with list items</returns>
    protected XmlNode LoadListItems(Lists listService, string listName)
    {
        // Get all SharePoint lists
        XmlNode listCollectionCaml = listService.GetListCollection();

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(listCollectionCaml.OwnerDocument.NameTable);

        nsmgr.AddNamespace("soap", "http://schemas.microsoft.com/sharepoint/soap/");

        // Check list with given name exists
        XmlNodeList collections = listCollectionCaml.SelectNodes("/soap:List[@Title = '" + listName + "']", nsmgr);

        // If specified list does not exists show error
        if (collections.Count == 0)
        {
            DisplayError(ResHelper.GetString("SharePoint.listdoesnotexists"));
            return(null);
        }


        #region "Advanced options"

        //XmlElement query = null;
        XmlDocument viewFields   = null;
        XmlDocument query        = null;
        XmlDocument queryOptions = null;

        // Specify search by query
        if (!String.IsNullOrEmpty(Query))
        {
            //query = xmlOptions.CreateElement("Query");
            //query.InnerXml = this.Query; //"<Where><Gt><FieldRef Name=\"ID\" /><Value Type=\"Counter\">0</Value></Gt></Where>";

            query = new XmlDocument();
            query.LoadXml(Query);
        }

        // Select item
        if (!String.IsNullOrEmpty(ItemIDField))
        {
            string queryValue = QueryHelper.GetText(ItemIDField, null);

            if (queryValue != null)
            {
                IsSelected = true;
                // Set field name from ItemIDField if not set
                string fieldName = (String.IsNullOrEmpty(SelectedItemFieldName)) ? ItemIDField : SelectedItemFieldName;

                // No query was set
                if (query == null)
                {
                    query = new XmlDocument();
                }

                // Get or create Query node
                XmlNode queryNode = query.SelectSingleNode("Query");
                if (queryNode == null)
                {
                    queryNode = query.CreateNode(XmlNodeType.Element, "Query", null);
                    query.AppendChild(queryNode);
                }

                // Get or create Where node
                XmlNode whereNode = queryNode.SelectSingleNode("Where");
                if (whereNode == null)
                {
                    whereNode = query.CreateNode(XmlNodeType.Element, "Where", null);
                    queryNode.AppendChild(whereNode);
                }

                // Prepare query for selecting item
                string selectItem = String.Format("<Eq><FieldRef ID='{0}' /><Value Type='{1}'>{2}</Value></Eq>", fieldName, ItemIDFieldType, queryValue);

                // Incorporate with original query
                if (!String.IsNullOrEmpty(whereNode.InnerXml))
                {
                    selectItem = String.Format("<And>{0}{1}</And>", whereNode.InnerXml, selectItem);
                }

                // Update Where node
                whereNode.InnerXml = selectItem;
            }
        }

        // Specify which fields should be retrieved
        if (!String.IsNullOrEmpty(ViewFields))
        {
            viewFields = new XmlDocument();
            string xml = "<ViewFields>";

            string[] fields = ViewFields.Split(';');
            foreach (string field in fields)
            {
                xml += string.Format("<FieldRef Name=\"{0}\" />", field.Trim());
            }

            xml += "</ViewFields>";
            viewFields.LoadXml(xml);
        }

        //xmlDoc.CreateElement("QueryOptions");
        //queryOptions.InnerXml = "";

        // If set limit maximum count of rows
        string rowLimit = (RowLimit > 0) ? RowLimit.ToString() : null;

        #endregion


        // Get documents in the list
        XmlNode documentsXml = listService.GetListItems(ListName, null, query, viewFields, rowLimit, queryOptions, null);

        return(documentsXml);
    }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();



            var listName = List.Title;
            var context  = this.ClientContext;
            var web      = context.Web;

            context.Load(web);
            context.ExecuteQueryRetry();
            string             webRelativeUrl = web.ServerRelativeUrl;
            ListCollection     allLists       = web.Lists;
            IEnumerable <List> foundLists     = context.LoadQuery(allLists.Where(list => list.Title == listName).Include(wl => wl.Id, wl => wl.Title, wl => wl.RootFolder.ServerRelativeUrl));

            context.ExecuteQueryRetry();



            var viewName         = Identity.Title;
            var internalRowLimit = Convert.ToUInt32(RowLimit.HasValue ? RowLimit.Value : 50);

            List listToUpdate = foundLists.FirstOrDefault();
            var  views        = listToUpdate.Views;

            ClientContext.Load(views, v => v.Include(tv => tv.Id,
                                                     tv => tv.Title,
                                                     tv => tv.ServerRelativeUrl,
                                                     tv => tv.DefaultView,
                                                     tv => tv.HtmlSchemaXml,
                                                     tv => tv.RowLimit,
                                                     tv => tv.Toolbar,
                                                     tv => tv.JSLink,
                                                     tv => tv.ViewFields,
                                                     tv => tv.ViewQuery,
                                                     tv => tv.Aggregations,
                                                     tv => tv.AggregationsStatus,
                                                     tv => tv.Hidden,
                                                     tv => tv.Method,
                                                     tv => tv.PersonalView,
                                                     tv => tv.ReadOnlyView,
                                                     tv => tv.ViewType));
            ClientContext.ExecuteQueryRetry();


            var thisview = Identity.GetView(listToUpdate);

            if (thisview != null)
            {
                Guid viewID = thisview.Id;


                if (ViewFields != null && ViewFields.Length > 0)
                {
                    var currentFields = thisview.ViewFields;
                    currentFields.RemoveAll();
                    ViewFields.ToList().ForEach(vField =>
                    {
                        currentFields.Add(vField.Trim());
                    });
                }

                if (JsLinkUris != null && JsLinkUris.Count() > 0)
                {
                    var jsLinkUri = String.Join("|", JsLinkUris);
                    thisview.JSLink = jsLinkUri;
                }
                thisview.RowLimit  = internalRowLimit;
                thisview.ViewQuery = QueryXml;

                if (Toolbar.HasValue)
                {
                    thisview.Toolbar = string.Format("<Toolbar Type=\"{0}\"/>", Toolbar.ToString());
                }

                if (this.ShouldProcess(string.Format("Should update view {0}", viewName)))
                {
                    thisview.Update();
                    listToUpdate.Context.ExecuteQueryRetry();
                }
            }
            else
            {
                var internalName = viewName.Replace(" ", string.Empty);

                if (this.ShouldProcess(string.Format("Should create the view {0}", viewName)))
                {
                    var view = listToUpdate.CreateView(internalName, ViewType.None, ViewFields, internalRowLimit, SetAsDefault, QueryXml, Personal, PagedView);
                    listToUpdate.Context.Load(view, v => v.Title, v => v.Id, v => v.ServerRelativeUrl);
                    listToUpdate.Context.ExecuteQueryRetry();

                    view.Title = viewName;

                    if (Toolbar.HasValue)
                    {
                        thisview.Toolbar = string.Format("<Toolbar Type=\"{0}\"/>", Toolbar.ToString());
                    }

                    if (JsLinkUris != null && JsLinkUris.Count() > 0)
                    {
                        var jsLinkUri = String.Join("|", JsLinkUris);
                        thisview.JSLink = jsLinkUri;
                    }

                    view.Update();
                    listToUpdate.Context.ExecuteQueryRetry();
                }
            }
        }
Exemplo n.º 9
0
 private IEnumerable <SPField> GetViewFields(SPList list)
 {
     return(ViewFields != null
                ? ViewFields.Select(viewField => list.Fields.GetField(viewField))
                : new List <SPField>());
 }
 public ViewDefinition WithViewContainingInternalName(string fieldName)
 {
     ViewFields.AndInternalName(fieldName);
     return(ViewFields);
 }
 public ViewDefinition WithViewContaining(string fieldDisplayName)
 {
     ViewFields.And(fieldDisplayName);
     return(ViewFields);
 }