sharepoint 获取自己发布的新闻

来源:互联网 发布:linux彻底删除用户所有 编辑:程序博客网 时间:2024/06/05 08:40

1)给列表"FilterNews"创建"标准视图"命名为"OwnNews",在"筛选栏"中选"创建者"、"等于"、"[本人]"。

2)用Microsoft Office SharePoint Designe打开Lists->FilterNews->OwnNews.aspx获取

筛选代码为:

<Query><Where><Eq><FieldRef Name="Author"/><Value Type="Integer"><UserID Type="Integer"/></Value></Eq></Where></Query>

在浏览器中显示代码为:

<Query><Where><Eq><FieldRef Name="Author"/><Value Type="Integer"><UserID Type="Integer"/></Value></Eq></Where></Query>

 

3)在VS创建WebPart中代码如下:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI.WebControls;

namespace FilterNewsTest
{
    [Guid("bb4222e6-2528-4bc9-b6b6-3952a54531e2")]
    public class FilterNewsWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _myProperty = null;
        private SPGridView sPGridView;
        private DataView dataView;

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Property Group")]
        [WebDisplayName("MyProperty")]
        [WebDescription("Meaningless Property")]
        public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "Hello SharePoint";
                }
                return _myProperty;
            }
            set { _myProperty = value; }
        }


        public FilterNewsWebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    sPGridView = new SPGridView();
                    sPGridView.AutoGenerateColumns = false;

                    BoundField colID = new BoundField();
                    colID.HeaderText = "ID";
                    colID.DataField = "ID";
                    sPGridView.Columns.Add(colID);

                    BoundField colTitle = new BoundField();
                    colTitle.HeaderText = "Title";
                    colTitle.DataField = "Title";
                    sPGridView.Columns.Add(colTitle);

                    BoundField colAuthor = new BoundField();
                    colAuthor.HeaderText = "Author";
                    colAuthor.DataField = "Author";
                    sPGridView.Columns.Add(colAuthor);

                    dataView = new DataView();
                    BindDataView();
                    sPGridView.DataSource = dataView;
                    sPGridView.DataBind();
                    base.CreateChildControls();

                    // Your code here...
                    this.Controls.Add(sPGridView);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
        private void BindDataView()
        {
            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists["FilterNews"];
            SPQuery query = new SPQuery();
            query.Query = "<Where><Eq><FieldRef Name='Author'/><Value Type='Integer'><UserID Type='Integer'/></Value></Eq></Where>";
            query.ViewFields = "<FieldRef Name='ID'/><FieldRef Name='Title'/><FieldRef Name='Author'/>";
            dataView = list.GetItems(query).GetDataTable().DefaultView;
        }
    }
}
4)发布WebPart。

5)完成。