Partager via


How To: SPS 2003: Access the columns from the “manage sites” list of “sites” area

TopologyManager topologyManager = new TopologyManager();
Uri uri = new Uri("<https://terminator:2004>");
PortalSiteCollection sites = topologyManager.PortalSites;
PortalContext portalContext = PortalApplication.GetContext(sites[uri]);
Guid homeGuid = AreaManager.GetSystemAreaGuid(portalContext, SystemArea.Home);
Area homeArea = AreaManager.GetArea(portalContext, homeGuid);
AreaCollection subareas = homeArea.Areas;
Area keywordArea = subareas["sites"];
SPWeb oweb  = keywordArea.Web;

These are the ref., i used for the above code
using System;
using System.Web;
using Microsoft.SharePoint ;
using Microsoft.SharePoint.Portal ;
using Microsoft.SharePoint.Portal.SiteData;
using Microsoft.SharePoint.Portal.Topology;
using Microsoft.SharePoint.Administration;

 

[1]
Whenever you add any new field to the list, it will be added at the end so if the last field’s id is 26 then the new field’s id will be 27. However, in this method you might have one risk, if any of the fields is deleted in between then the index number will go up.  Suppose for a new field(xyz), the id is 25 and if you delete one field at 22nd position then the xyz’s index number will become 24. Please take this situation in consideration if you want to go for hard coding the numbers.

[2]
You can get the unique name of the field by site settings -> right click on the column name -> properties -> URL(at the end, field’s internal name is written). You can decode the html encoding and use the name.

 

Also you can get the internal name of the field using the below code.

foreach (SPList list in lists)
{
if(list.Title == "tj")
                { 
                                foreach(SPField spf in list.Fields)
                                {
                                                MessageBox.Show(spf.Title + spf.InternalName);
                                }

                                SPListItem spl = list.Items[0];
                                MessageBox.Show(spl["test_x0020_2"].ToString()); // this is the internal name
                                MessageBox.Show(spl[16].ToString());
                                break;
                }
}