sharepoint面试问题,它有什么问题,还是有更好的方法来做到这一点0

时间:2011-08-10 19:33:01

标签: sharepoint web-parts

这就像是某人给我的面试问题,你能否告诉我这是错误的,或者是否有更好的办法。这将在一个大型企业中运行。注意:Demo.Tasks.Task是一个自定义对象,其定义未在此代码中显示。

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebControls;
using System.Collections;

namespace Demo.WebParts
{
    /// <summary>
    /// Description for MyTasks.
    /// </summary>
    [DefaultProperty("Text"),
        ToolboxData("<{0}:WebPart1 runat=server></{0}:WebPart1>"),
        XmlRoot(Namespace="Demo.Tasks")]
    public class MyTasks : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private const string defaultListName = "";
        private string listName = defaultListName;
        private ArrayList alTasks;

        [Browsable(true),
            Category("Miscellaneous"),
            DefaultValue(defaultListName),
            WebPartStorage(Storage.None),
            FriendlyName("List Name Filter"),
            Description("The name of sps lists to gather tasks from")]
        public string ListName
        {
            get
            {
                return listName;
            }

            set
            {
                listName = value;
            }
        }

        protected override void CreateChildControls()
        {
            alTasks = new ArrayList();
            try
            {
                SPWeb myWeb = SPControl.GetContextWeb(this.Context);
                foreach(SPList list in myWeb.Lists)
                {
                    if(list.BaseTemplate == SPListTemplateType.Tasks)
                    {
                        if(list.Title == listName)
                        {
                            getTasksFromList(list);
                        }
                    }
                }

                foreach(SPWeb subWeb in myWeb.Webs)
                    foreach(SPList list in subWeb.Lists)
                        if(list.BaseTemplate == SPListTemplateType.Tasks)
                            getTasksFromList(list);
            }
            catch
            {}
        }

        private void getTasksFromList(SPList list)
        {
            for(int i=0;i<list.ItemCount;i++)
            {
                if(list.Items[i]["AssignedTo"].ToString() == this.Context.User.Identity.Name)
                {
                    Demo.Tasks.Task tsk = Tasks.Task.CreateTask();
                    tsk.Title = list.Items[i]["Title"].ToString();
                    tsk.DueDate = (DateTime)list.Items[i]["DueDate"];
                    tsk.Description = list.Items[i]["Description"].ToString();
                    tsk.Priority = (int)list.Items[i]["Priority"];
                    alTasks.Add(tsk);

                    //now update the item
                    list.Items[i]["LastViewed"] = DateTime.Now;
                }
            }
        }

        /// <summary>
        /// Render this Web Part to the output parameter specified.
        /// </summary>
        /// <param name="output"> The HTML writer to write out to </param>
        protected override void RenderWebPart(HtmlTextWriter output)
        {
            try
            {
                string strHTML = "";
                for(int i=0;i<alTasks.Count;i++)
                {
                    Demo.Tasks.Task tsk = (Demo.Tasks.Task)alTasks[i];
                    strHTML = strHTML + "The task " + tsk.Title + " is due on " + tsk.DueDate + "<BR>" + tsk.Description + "<BR><BR>";
                }
                output.Write(strHTML);
            }
            catch
            {}
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果你给我们更多的信息(什么不起作用,等等)会更好。

但在我看来,当你使用SPWeb或SPSite时,你应该使用'使用'。不这样做有时会导致奇怪的错误:

using (SPWeb myWeb = SPControl.GetContextWeb(this.Context))
{
    foreach (SPList list in myWeb.Lists)
    {
        if (list.BaseTemplate == SPListTemplateType.Tasks)
        {
            if (list.Title == listName)
                getTasksFromList(list);
        }
    }
}

using (SPSite site = new SPSite(SiteUrl))
{
    foreach (SPWeb subWeb in site.AllWebs)
    {
        try //should be 1st statement after foreach
        {
            foreach (SPList list in subWeb.Lists)
            {
                if (list.BaseTemplate == SPListTemplateType.Tasks)
                    getTasksFromList(list);
            }
        }
        finally
        {
            if (subWeb != null)
                subWeb.Dispose();
        }
    }
}

在这里你可以读到这个: http://blogs.msdn.com/b/rogerla/archive/2009/01/14/try-finally-using-sharepoint-dispose.aspx