private static CrossListQueryInfo EmployeeCrossListQueryInfo()
        {
            CrossListQueryInfo clqi = new CrossListQueryInfo();

            // Insert the list types that you want to use. In this case, its the publishing page library (850, see code below)
            clqi.Lists = "<Lists ServerTemplate=\"100\" />";

            // Insert the field2s that you want to see. If there is a field inside that doesnt exist in the list that you query, your result will be nill, nada, nothing.
            // Make sure that you put in the INTERNAL field names!
            clqi.ViewFields = "<FieldRef Name=\"Last_x0020_Name\" /><FieldRef Name=\"First_x0020_Name\" /><FieldRef Name=\"Address\" /><FieldRef Name=\"Phone_x0020_Number\" />";

            // scop to use. Another possibility is SiteCollection
            clqi.Webs = "<Webs Scope=\"Recursive\" />";

            // turn the cache on
            clqi.UseCache = true;

            // if row limit == 0, you will get 0 results
            clqi.RowLimit = 100;

            // I know a stringbuilder would be better, but i wanted to show the markup of the query
            clqi.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";

            // put the CrossListQueryInfo object into the CrossListQueryCache
            //CrossListQueryCache clqc = new CrossListQueryCache(clqi);

            // and query the data!
            // make sure: the GetSiteData(SPWeb web) and GetSiteData(SPWeb web, SPSiteDataQuery query) DO NOT use caching!!!
            //DataTable tbl = clqc.GetSiteData(SPContext.Current.Site, CrossListQueryCache.ContextUrl());

            // return the datatable

            return clqi;
        }
Beispiel #2
0
        private static CrossListQueryInfo EmployeeCrossListQueryInfo()

        {
            CrossListQueryInfo clqi = new CrossListQueryInfo();

            // Insert the list types that you want to use. In this case, its the publishing page library (850, see code below)
            clqi.Lists = "<Lists ServerTemplate=\"100\" />";

            // Insert the field2s that you want to see. If there is a field inside that doesnt exist in the list that you query, your result will be nill, nada, nothing.
            // Make sure that you put in the INTERNAL field names!
            clqi.ViewFields = "<FieldRef Name=\"Last_x0020_Name\" /><FieldRef Name=\"First_x0020_Name\" /><FieldRef Name=\"Address\" /><FieldRef Name=\"Phone_x0020_Number\" />";

            // scop to use. Another possibility is SiteCollection
            clqi.Webs = "<Webs Scope=\"Recursive\" />";

            // turn the cache on
            clqi.UseCache = true;

            // if row limit == 0, you will get 0 results
            clqi.RowLimit = 100;

            // I know a stringbuilder would be better, but i wanted to show the markup of the query
            clqi.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";

            // put the CrossListQueryInfo object into the CrossListQueryCache
            //CrossListQueryCache clqc = new CrossListQueryCache(clqi);

            // and query the data!
            // make sure: the GetSiteData(SPWeb web) and GetSiteData(SPWeb web, SPSiteDataQuery query) DO NOT use caching!!!
            //DataTable tbl = clqc.GetSiteData(SPContext.Current.Site, CrossListQueryCache.ContextUrl());

            // return the datatable

            return(clqi);
        }
Beispiel #3
0
        static void testing2()
        {
            CrossListQueryInfo cLQI = new CrossListQueryInfo();
            CrossListQueryCache cLQC = new CrossListQueryCache(cLQI);

            //cLQC.GetSiteData()
        }
Beispiel #4
0
        static void testing2()
        {
            CrossListQueryInfo  cLQI = new CrossListQueryInfo();
            CrossListQueryCache cLQC = new CrossListQueryCache(cLQI);


            //cLQC.GetSiteData()
        }
Beispiel #5
0
        private void SPCrossListQeuryInfo_Click(object sender, EventArgs e)
        {
            using (SPSite _site = new SPSite("http://home/"))
            {
                using (SPWeb _web = _site.OpenWeb())
                {
                    CrossListQueryInfo query = new CrossListQueryInfo();
                    query.RowLimit = 100;
                    query.WebUrl = _web.ServerRelativeUrl;
                    query.Query = "<Where><Neq><FieldRef Name=\"ContentType\" /><Value Type=\"Text\"></Value></Neq></Where>";

                    ////////////////////////////////////////
                    /////// ------------------------- //////
                    /////// BaseType 	        Value //////
                    /////// ------------------------- //////
                    /////// Generic List	    0     //////
                    /////// Document Library	1     //////
                    /////// Discussion Board	3     //////
                    /////// Survey	            4     //////
                    /////// Issue	            5     //////
                    ////////////////////////////////////////

                    // Search in doclibs only
                    query.Lists = "<Lists BaseType='1' />";
                    // Only .doc files
                    query.Query =
                    @"<Where>
              <Eq>
            <FieldRef Name='DocIcon' />
            <Value Type='Computed'>docx</Value>
              </Eq>
            </Where>";

                    // Select only needed columns: file reference
                    query.ViewFields = "<FieldRef Name='FileRef' />";

                    // Search in all webs of the site collection

                    query.Webs = "<Webs Scope='SiteCollection' />";

                    CrossListQueryCache cache = new CrossListQueryCache(query);

                    // Perform the query
                    DataTable table = cache.GetSiteData(_web);
                    // Generate an absolute url for each document
                    foreach (DataRow row in table.Rows)
                    {
                        string relativeUrl =
                          row["FileRef"].ToString().Substring(
                            row["FileRef"].ToString().IndexOf("#") + 1);
                        string fullUrl = _site.MakeFullUrl(relativeUrl);

                        // Write urls to console
                        MessageBox.Show(fullUrl);
                    }
                }
            }
        }
Beispiel #6
0
        public static void ExampleOne(string siteURL)
        {
            using (SPSite site = new SPSite(siteURL))
            {
                //Get a CbqQueryVersionInfo object from a ContentByQueryWebPart and use it to get a CrossListQueryInfo
                ContentByQueryWebPart cbq         = new ContentByQueryWebPart();
                CbqQueryVersionInfo   versionInfo = cbq.BuildCbqQueryVersionInfo();
                CrossListQueryInfo    queryInfo   = versionInfo.VersionCrossListQueryInfo;
                // Create a new CrossListQueryCache object with the queryInfo we just got
                CrossListQueryCache crossListCache = new CrossListQueryCache(queryInfo);

                SiteDataResults results = crossListCache.GetSiteDataResults(site, false);
            }
        }
        //GetSiteDataResults(Microsoft.SharePoint.SPSite,System.Boolean)
        // Returns the results of the current query on the specified site.  The query will be run against
        // an SPList if useSpQueryOnList is set to true.  Otherwise the query will be run against a site or web
        // This version of GetSiteDataResults uses the web url in this object's CrossListQueryInfo to determine
        // either the web to query against or the web of the list being queried against depending on the value
        // of useSpQueryOnList.
        public static void Testing(string siteURL)
        {
            SPSite oSite = new SPSite(siteURL);
            SPWeb oWeb = oSite.RootWeb;

            string query = string.Empty;
            string websQuery = string.Format("<Webs Scope=\"{0}\"/>", "None");
            string lists = "<Lists ServerTemplate=\"101\"" + " ><List ID=\"4db0e78c-dd9c-45f2-8b9c-e5f602e7d0b7\" /></Lists>";
            bool useList = true;
            //string relativeUrl = this.GetRelativeUrl();
            //query = string.Format("<Where><Eq><FieldRef Name='FSObjType' /><Value Type='LookUp'>1</Value></Eq></Where>", relativeUrl);

            CrossListQueryInfo info = new CrossListQueryInfo();
            info.Lists = lists;
            info.Webs = websQuery;
            info.Query = query;
            info.ViewFields = "<FieldRef Name=\"FileLeafRef\"/>";
            info.WebUrl = oWeb.ServerRelativeUrl;
            CrossListQueryCache cache = new CrossListQueryCache(info);
            SiteDataResults sd = cache.GetSiteDataResults(oSite, true);
        }
Beispiel #8
0
        //GetSiteDataResults(Microsoft.SharePoint.SPSite,System.Boolean)
        // Returns the results of the current query on the specified site.  The query will be run against
        // an SPList if useSpQueryOnList is set to true.  Otherwise the query will be run against a site or web

        // This version of GetSiteDataResults uses the web url in this object's CrossListQueryInfo to determine
        // either the web to query against or the web of the list being queried against depending on the value
        // of useSpQueryOnList.
        public static void Testing(string siteURL)
        {
            SPSite oSite = new SPSite(siteURL);
            SPWeb  oWeb  = oSite.RootWeb;

            string query     = string.Empty;
            string websQuery = string.Format("<Webs Scope=\"{0}\"/>", "None");
            string lists     = "<Lists ServerTemplate=\"101\"" + " ><List ID=\"4db0e78c-dd9c-45f2-8b9c-e5f602e7d0b7\" /></Lists>";
            bool   useList   = true;
            //string relativeUrl = this.GetRelativeUrl();
            //query = string.Format("<Where><Eq><FieldRef Name='FSObjType' /><Value Type='LookUp'>1</Value></Eq></Where>", relativeUrl);

            CrossListQueryInfo info = new CrossListQueryInfo();

            info.Lists      = lists;
            info.Webs       = websQuery;
            info.Query      = query;
            info.ViewFields = "<FieldRef Name=\"FileLeafRef\"/>";
            info.WebUrl     = oWeb.ServerRelativeUrl;
            CrossListQueryCache cache = new CrossListQueryCache(info);
            SiteDataResults     sd    = cache.GetSiteDataResults(oSite, true);
        }
        private CrossListQueryInfo BuildCrossListQueryInfo()
        {
            var info = new CrossListQueryInfo
            {
                FilterByAudience    = this.FilterByAudience,
                GroupByAudience     = this.GroupBy == _audienceFieldColumnId,
                GroupByAscending    = this.GroupByDirection == SortDirection.Asc,
                ShowUntargetedItems = this.ShowUntargetedItems,
                UseCache            = this.UseCache
            };

            Type crossListQueryInfoType = typeof(CrossListQueryInfo);

            PropertyInfo requestThrottleOverrideInfo = crossListQueryInfoType.GetProperty("RequestThrottleOverride",
                                                                                          BindingFlags.NonPublic |
                                                                                          BindingFlags.Instance);

            requestThrottleOverrideInfo.SetValue(info, this.UseCache, null);

            if (this.ItemLimit >= 0)
            {
                info.RowLimit = !this.FilterByAudience ? ((uint)this.ItemLimit) : ((uint)(this.ItemLimit * 5));
            }
            else
            {
                info.RowLimit = uint.MaxValue;
            }

            Type       contentByQueryWebPart  = typeof(ContentByQueryWebPart);
            MethodInfo buildWebsElementMethod = contentByQueryWebPart.GetMethod("buildWebsElement",
                                                                                BindingFlags.NonPublic |
                                                                                BindingFlags.Instance |
                                                                                BindingFlags.Static |
                                                                                BindingFlags.InvokeMethod);

            info.Webs = string.IsNullOrEmpty(this.WebsOverride) ? (string)buildWebsElementMethod.Invoke(this, null) : this.WebsOverride;

            if (string.IsNullOrEmpty(this.ListsOverride))
            {
                MethodInfo buildListsElementMethod = contentByQueryWebPart.GetMethod("buildListsElement",
                                                                                     BindingFlags.NonPublic |
                                                                                     BindingFlags.Instance |
                                                                                     BindingFlags.Static |
                                                                                     BindingFlags.InvokeMethod);

                info.Lists = (string)buildListsElementMethod.Invoke(this, null);
            }
            else
            {
                MethodInfo getListIdMethod = contentByQueryWebPart.GetMethod("getListId",
                                                                             BindingFlags.NonPublic |
                                                                             BindingFlags.Instance |
                                                                             BindingFlags.Static |
                                                                             BindingFlags.InvokeMethod);

                string str = string.Format(CultureInfo.InvariantCulture, this.ListsOverride, new[] { getListIdMethod.Invoke(this, null) });
                info.Lists = str;
            }

            info.Query = string.IsNullOrEmpty(this.QueryOverride) ? this.BuildQueryElements() : this.QueryOverride;

            if (string.IsNullOrEmpty(this.ViewFieldsOverride))
            {
                MethodInfo buildViewFieldsElementsMethod = contentByQueryWebPart.GetMethod("buildViewFieldsElements",
                                                                                           BindingFlags.NonPublic |
                                                                                           BindingFlags.Instance |
                                                                                           BindingFlags.Static |
                                                                                           BindingFlags.InvokeMethod);

                info.ViewFields = (string)buildViewFieldsElementsMethod.Invoke(this, null);
                if (info.FilterByAudience || info.GroupByAudience)
                {
                    MethodInfo getCorrectFormatForFieldRefMethod = contentByQueryWebPart.GetMethod("getCorrectFormatForFieldRef",
                                                                                                   BindingFlags.NonPublic |
                                                                                                   BindingFlags.Instance |
                                                                                                   BindingFlags.Static |
                                                                                                   BindingFlags.InvokeMethod);

                    object[] parameters = { _audienceFieldColumnId };

                    info.AudienceFieldId = (string)getCorrectFormatForFieldRefMethod.Invoke(this, parameters);
                }
            }
            else
            {
                info.ViewFields = this.ViewFieldsOverride;

                FieldInfo fieldaddedViewFields = contentByQueryWebPart.GetField("addedViewFields", BindingFlags.NonPublic | BindingFlags.Instance);

                if (fieldaddedViewFields != null)
                {
                    if (((Hashtable)fieldaddedViewFields.GetValue(this)).Count == 0)
                    {
                        MethodInfo addViewFieldsFromOverrideMethod = contentByQueryWebPart.GetMethod("AddViewFieldsFromOverride",
                                                                                                     BindingFlags.NonPublic |
                                                                                                     BindingFlags.Instance |
                                                                                                     BindingFlags.Static |
                                                                                                     BindingFlags.InvokeMethod);

                        addViewFieldsFromOverrideMethod.Invoke(this, null);
                    }
                }
            }

            MethodInfo makeServerRelUrlMethod = contentByQueryWebPart.GetMethod("MakeServerRelUrl",
                                                                                BindingFlags.NonPublic |
                                                                                BindingFlags.Instance |
                                                                                BindingFlags.Static |
                                                                                BindingFlags.InvokeMethod);

            object[] makeServerParameters = { this.WebUrl };

            info.WebUrl = ((string)makeServerRelUrlMethod.Invoke(this, makeServerParameters)).TrimEnd(new[] { '/' });
            return(info);
        }
        private void IssueQuery()
        {
            Type      contentByQueryWebPart = typeof(ContentByQueryWebPart);
            FieldInfo fieldResults          = contentByQueryWebPart.GetField("results", BindingFlags.NonPublic | BindingFlags.Instance);

            if (fieldResults != null)
            {
                if (fieldResults.GetValue(this) == null)
                {
                    MethodInfo isConfiguredMethod = contentByQueryWebPart.GetMethod("IsConfigured",
                                                                                    BindingFlags.NonPublic |
                                                                                    BindingFlags.Instance |
                                                                                    BindingFlags.Static |
                                                                                    BindingFlags.InvokeMethod);

                    if (!(bool)isConfiguredMethod.Invoke(this, null))
                    {
                        var table = new DataTable
                        {
                            Locale = CultureInfo.InvariantCulture
                        };

                        fieldResults.SetValue(this, new SiteDataResults(table, false));
                    }
                    else
                    {
                        PropertyInfo errorTextInfo = contentByQueryWebPart.GetProperty("ErrorText",
                                                                                       BindingFlags.NonPublic |
                                                                                       BindingFlags.Instance);

                        try
                        {
                            CrossListQueryInfo queryCacheInfo = this.BuildCrossListQueryInfo();
                            string             logInfo        = Resources.GetString("CbqLogWebPartTitle") + this.Title;
                            fieldResults.SetValue(this, new CrossListQueryCache(queryCacheInfo, logInfo).GetSiteDataResults(SPContext.Current.Site, true));
                            if (this.ProcessDataDelegate != null)
                            {
                                this.Data = this.ProcessDataDelegate(this.Data);
                            }

                            PropertyInfo isEditModeInfo = contentByQueryWebPart.GetProperty("isEditMode",
                                                                                            BindingFlags.NonPublic |
                                                                                            BindingFlags.Instance);

                            if ((((SiteDataResults)fieldResults.GetValue(this)).Partial && (bool)isEditModeInfo.GetValue(this, null)) && SPContext.Current.Web.DoesUserHavePermissions(SPBasePermissions.EmptyMask | SPBasePermissions.AddAndCustomizePages))
                            {
                                Assembly server = Assembly.Load("Microsoft.Office.Server, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
                                Type     indexedListQueryExecutionContextType = server.GetType("Microsoft.Office.Server.Utilities.IndexedListQueryExecutionContext");

                                object indexedListQueryExecutionContextObject = Activator.CreateInstance(indexedListQueryExecutionContextType, null);

                                MethodInfo createSimplePartialResutlsWarningControlMethod = indexedListQueryExecutionContextType.GetMethod("CreateSimplePartialResutlsWarningControl",
                                                                                                                                           BindingFlags.NonPublic |
                                                                                                                                           BindingFlags.Instance |
                                                                                                                                           BindingFlags.Static |
                                                                                                                                           BindingFlags.InvokeMethod);

                                this.Controls.Add((Control)createSimplePartialResutlsWarningControlMethod.Invoke(indexedListQueryExecutionContextObject, null));
                            }
                        }
                        catch (SPQueryThrottledException)
                        {
                            errorTextInfo.SetValue(this, Resources.GetString("CbqErrorThrottled"), null);
                        }
                        catch (WebPartPageUserException)
                        {
                            errorTextInfo.SetValue(this, Resources.GetString("CbqErrorMalformedQuery"), null);
                        }
                        catch (SPException)
                        {
                            errorTextInfo.SetValue(this, Resources.GetString("CbqErrorMalformedQuery"), null);
                        }
                        catch (ArgumentException)
                        {
                            errorTextInfo.SetValue(this, Resources.GetString("CbqErrorMalformedQuery"), null);
                        }
                        catch (InvalidOperationException)
                        {
                            errorTextInfo.SetValue(this, Resources.GetString("CbqErrorMalformedQuery"), null);
                        }
                        catch (FileNotFoundException)
                        {
                            errorTextInfo.SetValue(this, Resources.GetString("CbqErrorMalformedQuery"), null);
                        }
                        catch (SqlException)
                        {
                            errorTextInfo.SetValue(this, Resources.GetString("CbqErrorBackend"), null);
                        }
                        catch (UnauthorizedAccessException)
                        {
                            errorTextInfo.SetValue(this, Resources.GetString("CbqErrorBackend"), null);
                        }
                        finally
                        {
                            if (fieldResults.GetValue(this) == null)
                            {
                                var table2 = new DataTable
                                {
                                    Locale = CultureInfo.InvariantCulture
                                };

                                fieldResults.SetValue(this, new SiteDataResults(table2, true));
                            }
                        }
                    }
                }
            }
        }
Beispiel #11
0
        protected void SearchButton_Click(object sender, EventArgs e)
        {
            var searchTitle       = TitleSearch.Text;
            var searchDescription = DescriptionSearch.Text;
            var searchPageCount   = PagesCountSearch.Text;

            var searchTitleTemplate       = $"<Contains><FieldRef Name='Title'></FieldRef><Value Type='Text'>{searchTitle}</Value></Contains>";
            var searchDescriptionTemplate = $"<Contains><FieldRef Name='Announcement'></FieldRef><Value Type='Text'>{searchDescription}</Value></Contains>";
            var searchPageCountTemplate   = $"<Eq><FieldRef Name='PagesCount'></FieldRef><Value Type='Number'>{searchPageCount}</Value></Eq>";

            var propsSearchArr         = new Stack();
            var totalSearchParamsCount = 0;

            if (!string.IsNullOrEmpty(searchTitle))
            {
                totalSearchParamsCount++;
                propsSearchArr.Push(searchTitleTemplate);
            }
            if (!string.IsNullOrEmpty(searchDescription))
            {
                totalSearchParamsCount++;
                propsSearchArr.Push(searchDescriptionTemplate);
            }
            if (!string.IsNullOrEmpty(searchPageCount))
            {
                totalSearchParamsCount++;
                propsSearchArr.Push(searchPageCountTemplate);
            }

            var camlQuery = string.Concat("<Where>",
                                          totalSearchParamsCount > 0 ? "<And>" : "",
                                          totalSearchParamsCount > 1 ? "<And>" : "",
                                          "<BeginsWith>",
                                          "<FieldRef Name='ContentTypeId'></FieldRef>",
                                          "<Value Type='ContentTypeId'>", searchContentType, "</Value>",
                                          "</BeginsWith>",
                                          totalSearchParamsCount > 0 ? propsSearchArr.Pop() : "",
                                          totalSearchParamsCount > 0 ? "</And>" : "",
                                          totalSearchParamsCount > 2 ? "<And>" : "",
                                          totalSearchParamsCount > 1 ? propsSearchArr.Pop() : "",
                                          totalSearchParamsCount > 2 ? propsSearchArr.Pop() : "",
                                          totalSearchParamsCount > 1 ? "</And>" : "",
                                          totalSearchParamsCount > 2 ? "</And>" : "",
                                          "</Where>",
                                          "<OrderBy>",
                                          "<FieldRef Name='Modified' Ascending='FALSE' />",
                                          "</OrderBy>");

            var siteQuery = new CrossListQueryInfo();

            siteQuery.Lists      = listsSearch;
            siteQuery.Query      = camlQuery;
            siteQuery.ViewFields = viewFieldsSearch;
            siteQuery.Webs       = websSearch;
            siteQuery.UseCache   = false;

            var siteQueryCache = new CrossListQueryCache(siteQuery);

            var results = siteQueryCache.GetSiteData(SPContext.Current.Site);

            foreach (DataRow row in results.Rows)
            {
                row["PagesCount"] = row["PagesCount"].ToString().Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)[0];
            }
            for (var i = results.Columns.Count - 1; i >= 0; i--)
            {
                switch (results.Columns[i].ColumnName)
                {
                case "Title":
                case "Announcement":
                case "PagesCount":
                    break;

                default:
                    results.Columns.RemoveAt(i);
                    break;
                }
            }

            ResultDocuments.DataSource = results;
            ResultDocuments.DataBind();
        }