SharePoint 2010 ile Farklı SiteCollection üzerindeki listeyi görüntülemek

Bildiğiniz gibi SharePoint üzerinde faklı "Site Collection" lar kullandığımız zaman yarattığımız içeriği diğer bir "Site Collection" altında göremiyoruz (kod yazmadan).

Bu işlemi yapmak için yeni bir webPart yazmamız gerekiyor. Bu webPart içinde ListViewByQuery isimli bir control kullanarak adresini, view'ini ve ismini verdiğimiz bir liste öğelerini listeleyebiliriz.

 

Daha kolay anlayabilmeniz için önce basit bir versiyonunu yazacağız:
 
        protected ListViewByQuery viewByQuery = null ;
        protected EncodedLiteral encodedLiteral = null;
        protected override void CreateChildControls()
        {
            SPSite site = null ;
            SPWeb web = null ;
            this .siteUrl = "Http://intranet" ;
            this .sourceList = "Grup Duyuruları" ;
            site = new SPSite ( this.SiteUrl);
            web = site.OpenWeb();
            SPList sourceList = web.Lists[this .SourceList];
            viewByQuery = new ListViewByQuery ();
            SPQuery query = null ;
            viewByQuery.List = sourceList;
            query = new SPQuery (viewByQuery.List.DefaultView);
            viewByQuery.Query = query;
            this .Controls.Add(viewByQuery);
        }

Yukarıdaki örnekte aşağıdaki öğelerin değerlerini el ile verdik:

siteURL : Listemizin bulunduğu SiteCollection'un adresi
sourceList :Liste adı
query  : Liste filtresi (örnekte DefaultView kullandık) 

 

Eğer Parametreleri webPart üzerinden değiştirebilmek ve Webpartı özelleştirilebilir yapmak istiyorsak Kod bütünü aşağıdaki gibi olmalıdır:

#region using statements

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
#endregion

namespace gosterici2.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : System.Web.UI.WebControls.WebParts.WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/gosterici2/gosterici2/gostericiII.ascx";

        #region protected child control variable definitions
        protected ListViewByQuery viewByQuery = null;
        protected EncodedLiteral encodedLiteral = null;
        #endregion

        #region webpart properties
        /// <summary>
        /// 
        /// </summary>
        private string viewNameField = string.Empty;
        [Personalizable(), WebPartStorage(Storage.Shared), WebBrowsable(),
         WebDisplayName("View Name"), WebDescription("View Name")]
        public string ViewName
        {
            get
            {
                return this.viewNameField;
            }
            set
            {
                this.viewNameField = value;
            }
        }
        
        /**********************************************************/
        /*Bir webparta property tanımlamak için gerekli kod*******/
        private string recepField = string.Empty;
        [Personalizable(), WebPartStorage(Storage.Shared), WebBrowsable(),
         WebDisplayName("İsminiz"), WebDescription("Adınızı yazınız")]
        public string RecepField
        {
            get
            {
                return this.recepField;
            }
            set
            {
                this.recepField = value;
            }
        }
        /**********************************************************/


        private string siteUrlField = string.Empty;
        [Personalizable(), WebPartStorage(Storage.Shared), WebBrowsable(),
         WebDisplayName("Site Url"), WebDescription("Site Url")]
        public string SiteUrl
        {
            get
            {
                return this.siteUrlField;
            }
            set
            {
                this.siteUrlField = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private string sourceListField = string.Empty;
        [Personalizable(), WebPartStorage(Storage.Shared), WebBrowsable(),
         WebDisplayName("Source List"), WebDescription("Source list to query")]
        public string SourceList
        {
            get
            {
                return this.sourceListField;
            }
            set
            {
                this.sourceListField = value;
            }
        }
        private Boolean disableFilterField = false;
        [Personalizable(), WebPartStorage(Storage.Shared), WebBrowsable(),
         WebDisplayName("Disable Filter"), WebDescription("Disable List Filtering")]
        public Boolean DisableFilter
        {
            get
            {
                return disableFilterField;
            }
            set
            {
                disableFilterField = value;
            }
        }
        private Boolean disableSortField = false;
        [Personalizable(), WebPartStorage(Storage.Shared), WebBrowsable(),
         WebDisplayName("Disable Sort"), WebDescription("Disable list sorting")]
        public Boolean DisableSort
        {
            get
            {
                return disableSortField;
            }
            set
            {
                this.disableSortField = value;
            }
        }
        #endregion

        #region overrides
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            SPSite site = null;
            SPWeb web = null;
            Boolean disposeSPSite = false;
            try
            {
                if (this.SourceList != string.Empty)
                {
                    viewByQuery = new ListViewByQuery();
                    if (this.SiteUrl != null && this.SiteUrl != string.Empty)
                    {
                        site = new SPSite(this.SiteUrl);
                        disposeSPSite = true;
                    }
                    else
                    {
                        site = SPContext.Current.Site;
                    }
                    web = site.OpenWeb();
                    SPList sourceList = web.Lists[this.SourceList];
                    viewByQuery.List = sourceList;
                    SPQuery query = null;
                    if (CheckIfViewExists(viewByQuery.List))
                    {
                        //use the view specified in webpart property
                        query = new SPQuery(viewByQuery.List.Views[this.ViewName]);
                    }
                    else
                    {
                        //use default view to initialized
                        query = new SPQuery(viewByQuery.List.DefaultView);
                    }
                    viewByQuery.Query = query;
                    viewByQuery.DisableFilter = this.DisableFilter;
                    viewByQuery.DisableSort = this.DisableSort;
                    
                    this.Controls.Add(viewByQuery);
                }
                else
                {
                    encodedLiteral = new EncodedLiteral();
                    encodedLiteral.Text = "This webpart is not configured.";
                    this.Controls.Add(encodedLiteral);
                }
            }
            finally
            {
                if (disposeSPSite)
                {
                    ((IDisposable)site).Dispose();
                    ((IDisposable)web).Dispose();
                }
            }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            EnsureChildControls();
            RenderChildren(writer);
        }
        #endregion

        #region helper methods
        private Boolean CheckIfViewExists(SPList list)
        {
            Boolean ret = false;
            foreach (SPView view in list.Views)
            {
                if (view.Title.ToLower() == this.ViewName.ToLower())
                {
                    ret = true;
                }
            }
            return ret;
        }
        #endregion
    }
}
 
Kaynak:http://blogs.msdn.com/b/ramg/archive/2009/04/22/implementing-a-simple-cross-site-collection-list-view-webpart.aspx

Add comment