views:

189

answers:

2

Well, as I am relatively new to Sharepoint 2007 I would like to improve my way of writing code for this platform, and refine some habits I already have.

In the following code what i am doing is to ask the size of a document that is in the "Documents" list, but i have the impression i create to many objects to access the info. Isn't there a way to go to the doc straight forward (i have to access de spweb, then the list and then the doc)?

One of the things I want to improve is not to use oSPWeb.Lists[0], which corresponds to oSPWeb.Lists["Documents"], non of them convinces me, because this list can be called "Documentos", o "Documents" etc...

Plz could you improve this code?

System.Text.StringBuilder oSb = new System.Text.StringBuilder();

               oSb.Append("     <Where>");
        oSb.Append("         <Eq>");
        oSb.Append("              <FieldRef Name=\"FileLeafRef\" />");
        oSb.Append("              <Value Type=\"Text\">"+documento+"</Value>");
        oSb.Append("         </Eq>");
        oSb.Append("     </Where>");
        oSb.Append("    <ViewFields>");
        oSb.Append("         <FieldRef Name=\"FileSizeDisplay\" />");
        oSb.Append("    </ViewFields>");    

        string sResult = oSb.ToString();

        bool Existe = false;
        SPSite sps = null;
        SPWeb oSPWeb = null;
        SPList oList = null;
        SPListItemCollection col = null;

        try
        {
            sps = SPContext.Current.Site;
            using(oSPWeb = sps.OpenWeb(url))
            {
                oList = oSPWeb.Lists[0];
                SPQuery qry = new SPQuery();
                qry.Query = sResult;

                col=oList.GetItems(qry);
            }
        }

        catch { }

        if (col != null)
        {

            //return col[0].File.Length.ToString();
            return col[0]["FileSizeDisplay"].ToString();
        }
        else
        {
            return null;
        }
+1  A: 

You're likely going to need to know the name, index, or ID of the document library because there can be several document libraries within a particular site. So, the name may be stored somewhere in a configuration location (could be another list with name-value pairs), but in the end you will have to do something like web.Lists["list name"] (or list Id - I'd avoid list index).

Using a CAML query (which you are doing) is the typical way to select multiple items from a list/library. My only recommendation there is to not assume there will be at least one item in the collection if the collection is not null. So instead of:

if (col != null)

I would do:

if (col != null && col.Count > 0)
Kirk Liemohn
If you'r storing information about how to fetch the list you should store the url and use web.GetList(url) instead. Read Scenario 1 for more info:http://blogs.msdn.com/sowmyancs/archive/2008/10/26/best-practices-sharepoint-object-model-for-performance-tuning.aspx
JMD
A: 

Finally, I have improved the code following Kirk's and JMD's advices, the url http://blogs.msdn.com/sowmyancs/archive/2008/10/26/best-practices-sharepoint-object-model-for-performance-tuning.aspx is really a very good start to optimize the code:

System.Text.StringBuilder oSb = new System.Text.StringBuilder();

            oSb.Append("     <Where>");
            oSb.Append("         <Eq>");
            oSb.Append("              <FieldRef Name=\"FileLeafRef\" />");
            oSb.Append("              <Value Type=\"Text\">"+document+"</Value>");
            oSb.Append("         </Eq>");
            oSb.Append("     </Where>");
            oSb.Append("    <ViewFields>");
            oSb.Append("         <FieldRef Name=\"FileSizeDisplay\" />");
            oSb.Append("    </ViewFields>");            

            string sResult = oSb.ToString();

            SPSite sps = null;
            SPWeb oSPWeb = null;
            SPList oList = null;
            SPListItemCollection col = null;

            try
            {
                sps = SPContext.Current.Site;
                using(oSPWeb = sps.OpenWeb(url))
                {
                    //oList = oSPWeb.Lists[0];
                    oList=oSPWeb.GetList(oSPWeb.Url + "/" + listName);
                    SPQuery qry = new SPQuery();
                    qry.Query = sResult;

                    col=oList.GetItems(qry);
                }
            }

            catch { }

            if (col != null && col.Count > 0)
            {
                return col[0]["FileSizeDisplay"].ToString();
            }
            else
            {
                return null;
            }
netadictos