Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

this is like an interview question given to me by someone, can you tell me whats wrong with it or if there is a better way of doing this. this will be run in a large enterprise. NOTE : Demo.Tasks.Task is a custom object whose deninition is not shown in this code.

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
            {}
        }
    }
}
share|improve this question

closed as not a real question by Rob D'Oria, Vedran Rasol, PirateEric, Anders Rask Aug 10 '11 at 21:04

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

add comment

2 Answers 2

if you want to know what is not good in this code, here are the first things I'm thinking of

foreach(SPWeb subWeb in myWeb.Webs)
       foreach(SPList list in subWeb.Lists)

-> SPWeb not disposed properly, consider using the Using statement or dispose it manually in a try/catch/finally block

list.Items[i]["LastViewed"] = DateTime.Now;

->will not take effect until you call Update() on the SPListItem

Empty try/catch statement hides an exception -> bad practice, better let it blows than doing nothing, you should rather handle and/or log the exception

 private void getTasksFromList(SPList list)
        {
            for(int i=0;i<list.ItemCount;i++)
            {
                if(list.Items[i]["AssignedTo"].ToString() == this.Context.User.Identity.Name)
                {

-> Wrong way to iterate through list.Items you should rather do something like that:

 private void getTasksFromList(SPList list)
        {
            SPListItemCollection items = list.items;
            for(int i=0;i<items.Count;i++)
            {
                SPListItem listItem = items[i];
                if(listItem["AssignedTo"].ToString() == this.Context.User.Identity.Name)
                {

I may have missed some stuff still

share|improve this answer
    
catch {} ? :-) –  Anders Rask Aug 10 '11 at 21:06
    
It's there -> Empty try/catch statement hides an exception :D –  GillouX Aug 10 '11 at 21:08
    
ah sorry... :-) –  Anders Rask Aug 10 '11 at 21:12
add comment

Use the CrossListQueryInfo + CAML query to retrieve the tasks. It's fast as hell, much faster then then the code that you wrote. According to some measures by Waldek Mastykarz (click click), it appears that the CLQI is fast incredibly fast. Using the SPSiteDataQuery may be an option of 2nd choice. Especially in large environments, this will be a good idea to use.

Info on how to use the CrossListQueryInfo can be found in my blogpost. Make sure to read the post entirely and make sure to make use of it correctly: wrong usage of the CrossListQueryInfo can cause some loss of performance, although it will still be faster than your code.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.