转载一篇关于 SP 中 DocumentDiscussion Feature 开发!

#region Copyright(c) 2007 李曦光(Bruce Lee). All Right Reserved.
/******************************************************************************
 * Copyright(c) 2007 李曦光(Bruce Lee). All Right Reserved.
 * 
 * 创建时间:2007-12-3
 * 作者:Bruce Lee
 * Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

  1.  Redistribution of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
  2.  Redistribution in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
  3.  The name of the author may not be used to endorse or promote products
      derived from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS AND ANY EXPRESS OR IMPLIED
   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
   EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
#endregion
using System;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using System.Text;

namespace BruceLee.DocumentDiscussion
{
    public partial class Discussion : System.Web.UI.Page
    {
        #region 变量
        string ListID = string.Empty;
        string SiteURL = string.Empty;
        string strUserName = string.Empty;
        const string strField = "BruceLeeDiscussion";
        int ItemID;
        const string strBruceLeeDocumentDiscussion = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?><BruceLeeDocumentDiscussion></BruceLeeDocumentDiscussion>";
        #endregion

        #region 页面加载
        protected void Page_Load(object sender, EventArgs e)
        {
            strUserName = GetUserName();
            txtbCreateTiem.Text = DateTime.Now.ToString();
            txtbAuthor.Text = strUserName;
            SiteURL = Request.Params["SiteURL"];
            if (SiteURL.Length > 0)
                if (SiteURL.IndexOf('/', 7) == -1)
                    SiteURL = "/";
                else
                    SiteURL = SiteURL.Substring(SiteURL.LastIndexOf('/'));
            ListID = Request.Params["ListID"];
            if (ListID.Length == 38 || ListID.Length == 36)
                ListID = new Guid(ListID).ToString();
            ItemID = System.Convert.ToInt32(Request.Params["ItemID"]);
            labList.Text = ListID.ToString();
            labItem.Text = ItemID.ToString();
            labSiteUrl.Text = SiteURL.ToString();
            if (!IsPostBack)
            {
                SPList spList = GetList(SiteURL, ListID);
                SPListItem spListItem = GetListItem(spList, ItemID);
                if (!IsHaveField(spList, strField))
                {
                    CreateField(spList, spListItem, strField, ItemID);
                }
                gvDataBind(spList, strField, ItemID);
            }
        }
        #endregion

        #region 得到列表
        /// <summary>
        /// 得到列表
        /// </summary>
        /// <param name="SiteURL">站点URL</param>
        /// <param name="ListName">列表名称</param>
        /// <returns></returns>
        protected SPList GetList(string SiteURL, string ListName)
        {
            SPList spList;
            using (SPWeb spWeb = SPContext.Current.Site.OpenWeb(SiteURL))
            {
                spWeb.Site.AllowUnsafeUpdates = true;
                spWeb.AllowUnsafeUpdates = true;
                if ((ListName.Trim().Length == 36) && (new Guid(ListName).ToString() == ListName))
                {
                    spList = spWeb.Lists[new Guid(ListName)];
                }
                else
                {
                    spList = spWeb.Lists[ListName];
                }
                return spList;
            }
        }
        #endregion

        #region 得到列表项
        /// <summary>
        /// 得到列表项
        /// </summary>
        /// <param name="spList">列表</param>
        /// <param name="strItemId">列表ID</param>
        /// <returns></returns>
        protected SPListItem GetListItem(SPList spList, int strItemId)
        {
            SPListItem spListItem = null;
            try
            {
                spListItem = spList.GetItemById(strItemId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return spListItem;
        }
        #endregion

        #region 判断列表的某个字段是否存在
        /// <summary>
        /// 判断列表的某个字段是否存在
        /// </summary>
        /// <param name="spList">列表</param>
        /// <param name="strField">字段名</param>
        /// <returns></returns>
        protected bool IsHaveField(SPList spList, string strField)
        {
            bool boolReturn = false;
            try
            {
                boolReturn = spList.Fields.ContainsField(strField);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return boolReturn;
        }
        #endregion

        #region 创建字段
        /// <summary>
        /// 创建字段
        /// </summary>
        /// <param name="spList">列表名称</param>
        /// <param name="strField">字段名称</param>
        /// <returns></returns>
        protected bool CreateField(SPList spList, SPListItem splistItem, string strField, int strItemId)
        {
            bool boolReturn = false;
            try
            {
                spList.ParentWeb.AllowUnsafeUpdates = true;

                spList.Fields.Add (strField,SPFieldType.Note,false);
                SPFieldMultiLineText textField = new SPFieldMultiLineText (spList.Fields,strField);
                textField.UnlimitedLengthInDocumentLibrary = true;
                textField.DefaultValue = strBruceLeeDocumentDiscussion;
                textField.Hidden = true;
                textField.ReadOnlyField = true;
                textField.ShowInDisplayForm = false;
                textField.ShowInEditForm = false;
                textField.ShowInListSettings = false;
                textField.ShowInNewForm = false;
                textField.ShowInVersionHistory = false;
                textField.ShowInViewForms = false;
                textField.Update();
                spList.ForceCheckout = true;
                spList.Update();
                
                //UpdateListItemValue(spListItem, strField, strBruceLeeDocumentDiscussion);
                boolReturn = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return boolReturn;
        }
        #endregion

        #region 更新列表字段值
        protected bool UpdateListItemValue(SPListItem spListItem, string strField, string strFieldValue)
        {
            bool returnValue = false;
            try
            {
                string strCheckOutStatus = string.Empty;
                spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = true;
                spListItem[strField] = strFieldValue;
                strCheckOutStatus = spListItem.File.CheckOutStatus.ToString ().ToUpper();
                if (strCheckOutStatus == "NONE" || strCheckOutStatus == "LONGTERMOFFLINE")
                {
                    spListItem.File.CheckOut();
                    strCheckOutStatus = "LONGTERM";
                    spListItem.Update();
                }
                if (strCheckOutStatus != "NONE")
                    spListItem.File.CheckIn(strUserName + "添加了评论");
                returnValue = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returnValue;
        }
        #endregion

        #region 评论提交
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string txtId,txtAuthor, txtCreateTime, txtContent, strTemp;
            string strContent = string.Empty;
            string strInsert = string.Empty;

            try
            {
                txtId = Guid.NewGuid().ToString();
                strTemp = "</BruceLeeDocumentDiscussion>";
                txtAuthor = txtbAuthor.Text.ToString();
                txtCreateTime = txtbCreateTiem.Text.ToString();
                txtContent = txtbContent.Text.ToString();

                SPList spList = GetList(SiteURL, ListID);
                SPListItem spListItem = GetListItem(spList, ItemID); ;
                spList.ParentWeb.AllowUnsafeUpdates = true;

                strContent = spListItem[strField].ToString();
                strContent = strContent.Replace(strTemp, "");
                strInsert = @"<Discussion Id=""" + txtId + @"""><Author>" + txtAuthor + "</Author><Content>" + txtContent + "</Content><CreateTiem>" + txtCreateTime + "</CreateTiem></Discussion>";
                strContent += strInsert + strTemp;
                UpdateListItemValue(spListItem, strField, strContent);
                gvDataBind(spList, strField, ItemID);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion

        #region 数据绑定
        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="spList">列表</param>
        /// <param name="strField">字段名称</param>
        /// <param name="strItemId">列表项ID</param>
        protected void gvDataBind(SPList spList, string strField, int strItemId)
        {
            string strContent = string.Empty;
            SPListItem spListItem = null;
            try
            {
                spListItem = GetListItem(spList, strItemId);
                if (spListItem[strField] != null)
                {
                    strContent = spListItem[strField].ToString();
                }
                else
                {
                    strContent = strBruceLeeDocumentDiscussion;
                    UpdateListItemValue(spListItem, strField, strBruceLeeDocumentDiscussion);
                }

                XmlDocument xmlContent = new System.Xml.XmlDocument();
                xmlContent.LoadXml(strContent);
                xmldsDiscussion.Data = xmlContent.InnerXml;
                xmldsDiscussion.XPath = "/BruceLeeDocumentDiscussion/Discussion";
                gvDiscussion.DataSource = xmldsDiscussion;
                gvDiscussion.DataKeyNames = new string[] { "Id" };
                gvDiscussion.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取用户名
        protected string GetUserName()
        {
            SPWeb spWeb = SPControl.GetContextWeb(Context);
            String strUserName = spWeb.CurrentUser.Name;
            String strCurrentUser = strUserName.Substring(strUserName.LastIndexOf(@"\") + 1);
            return strCurrentUser;
        }
        #endregion

        #region 删除评论
        protected void gvDiscussion_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string strContent = string.Empty;
                string strId = string.Empty;
                if (strUserName == "系统帐户")
                {
                    SPList spList = GetList(SiteURL, ListID);
                    SPListItem spListItem = GetListItem(spList, ItemID);
                    strContent = spListItem[strField].ToString();
                    XmlDocument xmlContent = new System.Xml.XmlDocument(); ;
                    xmlContent.LoadXml(strContent);

                    strId = gvDiscussion.DataKeys[e.RowIndex].Value.ToString();
                    string strXpathNode = String.Format("/BruceLeeDocumentDiscussion/Discussion[@Id='{0}']", strId);
                    xmlContent.SelectSingleNode("/BruceLeeDocumentDiscussion").RemoveChild(xmlContent.SelectSingleNode(strXpathNode));
                    UpdateListItemValue(spListItem, strField, xmlContent.InnerXml.ToString());
                    Response.Write("<script language='javascript'>alert('记录已被删除'); </script> "); 
                    gvDataBind(spList, strField, ItemID);
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('你不是管理员,不能删除评论'); </script >");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}

 

-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>文档讨论</title>
</head>
<body>
    <form >
    <div>
        <table width="100%" border="1" cellspacing="0" cellpadding="0" bordercolor="#111111" style="FONT-SIZE: 12px; BORDER-COLLAPSE: collapse">
            <tr align="center">
                <td colspan="3">
                    文档评论</td>
            </tr>
            <tr>
                <td style="width: 8%;">
                    <asp:Label ID="labAuthor" runat="server" Text="作者:" meta:resourcekey="labAuthorResource1"></asp:Label></td>
                <td colspan="2" style="width: 92%">
                    <asp:TextBox ID="txtbAuthor" runat="server" Enabled="False" BorderStyle="Groove" meta:resourcekey="txtbAuthorResource1"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 8%;">
                    <asp:Label ID="labContent" runat="server" Text="评论内容:" meta:resourcekey="labContentResource1"></asp:Label></td>
                <td colspan="2" style="width: 92%;">
                    <asp:TextBox ID="txtbContent" runat="server" TextMode="MultiLine" BorderStyle="Groove" Width="389px" Height="85px" meta:resourcekey="txtbContentResource1"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 8%;">
                    <asp:Label ID="labCreateTiem" runat="server" Text="创建时间:" meta:resourcekey="labCreateTiemResource1"></asp:Label></td>
                <td colspan="2" style="width: 92%;">
                    <asp:TextBox ID="txtbCreateTiem" runat="server" Enabled="False" BorderStyle="Groove" meta:resourcekey="txtbCreateTiemResource1"></asp:TextBox></td>
            </tr>
            <tr>
                <td align="center" colspan="3">
                    &nbsp;<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="提交" meta:resourcekey="btnSubmitResource1" />
                    &nbsp;&nbsp;
                    <asp:Button ID="btnClose" runat="server" Text="关闭" OnClientClick="javascript:window.close()" meta:resourcekey="btnCloseResource1" /></td>
            </tr>
        </table>
        <br />
        <asp:GridView ID="gvDiscussion" runat="server" AutoGenerateColumns="False" style="FONT-SIZE: 12px; BORDER-COLLAPSE: collapse" BorderColor="#111111" BorderWidth="1px" meta:resourcekey="gvDiscussionResource1" OnRowDeleting="gvDiscussion_RowDeleting">
            <Columns>
                <asp:TemplateField HeaderText="作者" meta:resourcekey="TemplateFieldResource1">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" meta:resourcekey="Label1Resource1"><%# XPath("Author") %></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="15%" Wrap="False" BorderColor="#111111" BorderWidth="1px" />
                    <HeaderStyle BorderColor="#111111" BorderWidth="1px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="内容" meta:resourcekey="TemplateFieldResource2">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" meta:resourcekey="Label2Resource1"><%# XPath("Content") %></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="60%" Wrap="False" BorderColor="#111111" BorderWidth="1px" />
                    <HeaderStyle BorderColor="#111111" BorderWidth="1px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="时间" meta:resourcekey="TemplateFieldResource3">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" meta:resourcekey="Label3Resource1"><%# XPath("CreateTiem") %></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="15%" Wrap="False" BorderColor="#111111" BorderWidth="1px" />
                    <HeaderStyle BorderColor="#111111" BorderWidth="1px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="删除" ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="删除"></asp:LinkButton>
                    </ItemTemplate>
                    <HeaderStyle BorderColor="#111111" />
                    <ItemStyle Width="10%" Wrap="False" BorderColor="#111111" />
                </asp:TemplateField>
            </Columns>
            <RowStyle BorderWidth="1px" BorderColor="#111111" BorderStyle="None" />
            <HeaderStyle BorderColor="#111111" BorderWidth="1px" />
        </asp:GridView>
        <asp:XmlDataSource ID="xmldsDiscussion" runat="server" EnableCaching="False">
        </asp:XmlDataSource>
        <asp:Label ID="labList" runat="server" Visible="False" meta:resourcekey="labListResource1"></asp:Label>
        <asp:Label ID="labSiteUrl" runat="server" Visible="False" meta:resourcekey="labSiteUrlResource1"></asp:Label>
        <asp:Label ID="labItem" runat="server" Visible="False" meta:resourcekey="labItemResource1"></asp:Label></div>
    </form>
</body>
</html>

 

?>
<BruceLeeDocumentDiscussion>
	<Discussion Id="11">
		<Author>11</Author>
		<Content>22</Content>
		<CreateTiem>33</CreateTiem>
	</Discussion>
</BruceLeeDocumentDiscussion>

FeatureFile:

?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
	<CustomAction
	  Id="F25C09F7-0B16-4f80-BDDD-C31181774C47"
	  RegistrationType="List"
	  RegistrationId="101"
	  ImageUrl="/_layouts/images/32316.GIF"
	  Location="EditControlBlock"
	  ShowInLists = "False"
	  Sequence="106"
	  Title="Document Discussion">
		<UrlAction Url="javascript:window.open('/_layouts/BruceLeeDocumentDiscussion/Discussion.aspx?SiteURL={SiteUrl}&amp;ListID={ListId}&amp;ItemID={ItemId}')" />
	</CustomAction>
</Elements>

 

?>
<Feature Id="B8ECC2E2-1F0C-4c06-8425-1A1581BAB540"
    Title="Document Discussion"
    Description="This Document Discussion,Welcome used"
    Version="1.0.0.0"
    Scope="Site" 
    AlwaysForceInstall="TRUE"
    xmlns="http://schemas.microsoft.com/sharepoint/">
	<ElementManifests>
		<ElementManifest Location="DocumentDiscussion.xml" />
	</ElementManifests>
</Feature>

@rem======================================================================
@rem
@rem    setupfeature.bat
@rem
@rem======================================================================

@echo off
setlocal
pushd .

goto InstallFeature


@rem----------------------------------------------------------------------
@rem   InstallFeature
@rem----------------------------------------------------------------------
:InstallFeature
    set SPAdminTool=%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN\stsadm.exe
    set TargetUrl=http://youloalhost:3000
    set FeaturePath=BruceLeeDocumentDiscussion\Feature.xml
    set CopyFeaturePath=%CommonProgramFiles%\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\DocumentDiscussion
   
    echo DeActivating feature %FeaturePath% 
    "%SPAdminTool%" -o deactivatefeature -filename %FeaturePath% -url %TargetUrl% -force
    
    echo UnInstallFeature %FeaturePath% 
	"%SPAdminTool%" -o uninstallfeature -filename %FeaturePath% -force

    echo InstallFeature %FeaturePath% 
    "%SPAdminTool%" -o installfeature -filename %FeaturePath%

    echo Activating feature %FeaturePath% 
    "%SPAdminTool%" -o activatefeature -filename %FeaturePath% -url %TargetUrl%

    echo iisreset
    iisreset
    pause
discussionboard

相关文章:

  • 2022-12-23
  • 2022-01-27
  • 2022-12-23
  • 2022-01-17
  • 2021-07-28
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
  • 2021-12-04
  • 2022-12-23
  • 2021-09-06
  • 2022-02-05
相关资源
相似解决方案