Benjamins Blog

Benjamin Wegner über Weisheiten, SharePoint und IT Zeugs

Benjamins Blog header image 2

Datenzugriff in SharePoint

Mai 27th, 2011 ·

Folgende drei Methoden können genutzt werden um Daten in SharePoint abzufragen:

        private void GetBySPQuery()
        {
            this.Controls.Add(new LiteralControl("GetBySPQuery<br />"));

            SPWeb spWeb = SPControl.GetContextWeb(Context);

            SPList list = spWeb.Lists["Tasks"];
            StringBuilder sb = new StringBuilder();

            SPQuery query = new SPQuery();
            query.Query = "<OrderBy><FieldRef Name='Created' /></OrderBy>";

            SPListItemCollection result = list.GetItems(query);

            foreach (DataRow r in result.GetDataTable().Rows)
            {
                sb.Append(r["Title"].ToString() + "<br /><br />");
            }

            this.Controls.Add(new LiteralControl(sb.ToString()));
        }

        private void GetBySPSiteDataQuery()
        {
            this.Controls.Add(new LiteralControl("GetBySPSiteDataQuery<br />"));
            SPWeb spWeb = SPControl.GetContextWeb(Context);

            SPSiteDataQuery siteQuery = new SPSiteDataQuery();
            siteQuery.Webs = "<Webs Scope='SiteCollection' />";
            siteQuery.Lists = "<Lists BaseType='1'/>";
            siteQuery.Query = "<OrderBy><FieldRef Name='Created' /></OrderBy>";
            siteQuery.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Created' />";

            StringBuilder sb = new StringBuilder();

            DataTable sqResult = spWeb.GetSiteData(siteQuery);
            foreach (DataRow r in sqResult.Rows)
            {
                sb.Append("ListID: " + r[0].ToString() + "<br />"); //guid
                sb.Append("WebID: " + r[1].ToString() + "<br />");  //guid
                sb.Append("ID: " + r[2].ToString() + "<br />");     //int
                sb.Append(r["Title"].ToString() + "<br /><br />");
            }

            this.Controls.Add(new LiteralControl(sb.ToString()));
        }

        private void GetByCrossListQueryInfo()
        {
            this.Controls.Add(new LiteralControl("GetByCrossListQueryInfo<br />"));

            SPWeb spWeb = SPControl.GetContextWeb(Context);

            CrossListQueryInfo clQuery = new CrossListQueryInfo();
            clQuery.Webs = "<Webs Scope='SiteCollection' />";
            clQuery.Lists = "<Lists BaseType='1'/>";
            clQuery.Query = "<OrderBy><FieldRef Name='Created' /></OrderBy>";
            clQuery.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Created' />";

            CrossListQueryCache clCache = new CrossListQueryCache(clQuery);

            StringBuilder sb = new StringBuilder();

            DataTable sqResult = clCache.GetSiteData(spWeb.Site);
            foreach (DataRow r in sqResult.Rows)
            {
                sb.Append("ListID: " + r[0].ToString() + "<br />"); //guid
                sb.Append("WebID: " + r[1].ToString() + "<br />");  //guid
                sb.Append("ID: " + r[2].ToString() + "<br />");     //int
                sb.Append(r["Title"].ToString() + "<br /><br />");
            }

            this.Controls.Add(new LiteralControl(sb.ToString()));
        }

Tags: Allgemein