PortalSiteMapProvider provides a nice automatic caching infrastructure for retrieving list data. The class includes a method called GetCachedListItemsByQuery that was used in this test. This method first retrieves data from a list based on an SPQuery object that is provided as a parameter to the method call. The method then looks in its cache to see if the items already exist. If they do, the method returns the cached results, and if not, it queries the list, stores the results in cache and returns them from the method call.
SPQuery strQuery = new SPQuery();
strQuery.RowLimit = 1;
//Retrieve the items for the specified Target location from the Target list
strQuery.Query = "CAML query...";
PortalSiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;
PortalWebSiteMapNode pNode = ps.FindSiteMapNode(web.ServerRelativeUrl) as PortalWebSiteMapNode;
SiteMapNodeCollection pItems = new SiteMapNodeCollection();
pItems = ps.GetCachedListItemsByQuery(pNode, "targetListName", strQuery, web);
//Check if there are items for the specified target location
if (pItems.Count != 0)
{
try
{
//Retrieve the most recently created item for the specified Target location from the list
foreach (PortalListItemSiteMapNode pItem in pItems)
{//TODO}
catch (Exception ex)
{}
}
Friday, March 12, 2010
Tuesday, March 2, 2010
Content types and Site Columns in MOSS 2007
Site column is a reusable component in sharepoint.
Deleting Columns
If a site collection contains lists that contain a site column you want to delete, you cannot delete that site column. If you delete a column from a list, that column is also deleted from any list content type on that list. When you delete a column, all data associated with that column is also deleted.
Site column usage in sharepoint:
There are three ways to use site columns in sharepoint and they are:
1.Using the central administration tool of sharepoint.
2.Using the WSS object model through code snippet.
3.Using features(XML file).
Using features to add site columns in the sharepoint site.
Feature.xml
The feature.xml file is a general description of the feature and looks like this:
Title="MyFields"
Description="Fields added through feature"
Version="12.0.0.0"
Scope="Site"
xmlns="http://schemas.microsoft.com/sharepoint/">http://schemas.microsoft.com/sharepoint/>
Site column feature xml
The file fields.xml looks like this:
Name="RegionFromFeature"
Group="MyColumns"
Type="Choice"
DisplayName="Region"
SourceID=http://schemas.microsoft.com/sharepoint/v3/fields
StaticName="RegionFromFeature"
FillInChoice="FALSE">
Global
Europe
Global
Use STSADM command to install and activate the feature and do an IISRESET.
Commands are:
stsadm -o installfeature -filename fields\feature.xml
stsadm -o activatefeature -filename fields\feature.xml -url “Site Collection URL”
Method for using the WSS object model to add a new site column:
SPSite site=new SPSite(“http://servername/myPortal”)”;
SPWeb web=site.OpenWeb(“/Topics”);
String newFld=web.Fields.Add(“Location”,SPFieldType.Choice,True);
newFld.Choices.Add(“Hyderabad”);
newFld.Choices.Add(“Banglore”);
newFld.Choices.Add(“Jamshedpur”);
newFld.DefaultValue=”Jamshedpur”;
newFld.Update();
Method for using the WSS object model to add a site column to a list:
SPSite site=new SPSite(“http://servername/myPortal”);
SPWeb web=site.OpenWeb(“Topics/MOSS2007”);
SPList list=web.Lists[“Shared Documents”];
SPField myFld=web.AvailableFields[“MyField”];
List.Fields.Add(MyField);
Add site columns to Content type using WSS object model:
SPSite site = new SPSite("http://servername/myPortal");
SPWeb web = site.OpenWeb("/Topics");
// Create new content type
SPContentType documentContentType = web.AvailableContentTypes["Document"];
SPContentType newContentType = new SPContentType(documentContentType, web.ContentTypes, "Address Document");
web.ContentTypes.Add(newContentType);
newContentType = web.ContentTypes[newContentType.Id];
// Add FieldLink to content type
SPField myFld = web.AvailableFields["myField"];
SPFieldLink fieldLink = new SPFieldLink(myFld);
newContentType.FieldLinks.Add(fieldLink);
newContentType.Update(false);
The above code snippet shows how to add site columns to a content Type. Here just add a new SPFieldLink object to the FieldLinks collection of the contenttype.
Deleting Columns
If a site collection contains lists that contain a site column you want to delete, you cannot delete that site column. If you delete a column from a list, that column is also deleted from any list content type on that list. When you delete a column, all data associated with that column is also deleted.
Site column usage in sharepoint:
There are three ways to use site columns in sharepoint and they are:
1.Using the central administration tool of sharepoint.
2.Using the WSS object model through code snippet.
3.Using features(XML file).
Using features to add site columns in the sharepoint site.
Feature.xml
The feature.xml file is a general description of the feature and looks like this:
Description="Fields added through feature"
Version="12.0.0.0"
Scope="Site"
xmlns="http://schemas.microsoft.com/sharepoint/">http://schemas.microsoft.com/sharepoint/>
Site column feature xml
The file fields.xml looks like this:
Group="MyColumns"
Type="Choice"
DisplayName="Region"
SourceID=http://schemas.microsoft.com/sharepoint/v3/fields
StaticName="RegionFromFeature"
FillInChoice="FALSE">
Use STSADM command to install and activate the feature and do an IISRESET.
Commands are:
stsadm -o installfeature -filename fields\feature.xml
stsadm -o activatefeature -filename fields\feature.xml -url “Site Collection URL”
Method for using the WSS object model to add a new site column:
SPSite site=new SPSite(“http://servername/myPortal”)”;
SPWeb web=site.OpenWeb(“/Topics”);
String newFld=web.Fields.Add(“Location”,SPFieldType.Choice,True);
newFld.Choices.Add(“Hyderabad”);
newFld.Choices.Add(“Banglore”);
newFld.Choices.Add(“Jamshedpur”);
newFld.DefaultValue=”Jamshedpur”;
newFld.Update();
Method for using the WSS object model to add a site column to a list:
SPSite site=new SPSite(“http://servername/myPortal”);
SPWeb web=site.OpenWeb(“Topics/MOSS2007”);
SPList list=web.Lists[“Shared Documents”];
SPField myFld=web.AvailableFields[“MyField”];
List.Fields.Add(MyField);
Add site columns to Content type using WSS object model:
SPSite site = new SPSite("http://servername/myPortal");
SPWeb web = site.OpenWeb("/Topics");
// Create new content type
SPContentType documentContentType = web.AvailableContentTypes["Document"];
SPContentType newContentType = new SPContentType(documentContentType, web.ContentTypes, "Address Document");
web.ContentTypes.Add(newContentType);
newContentType = web.ContentTypes[newContentType.Id];
// Add FieldLink to content type
SPField myFld = web.AvailableFields["myField"];
SPFieldLink fieldLink = new SPFieldLink(myFld);
newContentType.FieldLinks.Add(fieldLink);
newContentType.Update(false);
The above code snippet shows how to add site columns to a content Type. Here just add a new SPFieldLink object to the FieldLinks collection of the contenttype.
Subscribe to:
Posts (Atom)