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;
}