Which code segment should you use?

You have a SharePoint farm that has more than 100 custom Features.

You upgrade several Features in the farm.

You need to ensure that the site collection uses the most up-to-date versions of the Features. Only Features that require an upgrade must be evaluated.

Which code segment should you use?

You have a SharePoint farm that has more than 100 custom Features.

You upgrade several Features in the farm.

You need to ensure that the site collection uses the most up-to-date versions of the Features. Only Features that require an upgrade must be evaluated.

Which code segment should you use?

A.
SPWebServiceCollection webServices = new SPWebServiceCollection(SPFarm.Local);
foreach (SPWebService myWebService1 in webServices)
{
SPFeatureQueryResultCollection queryResults = myWebService1.QueryFeatures(SPFeatureScope.Site, true);
IEnumerator<SPFeature> featureEnumerator = queryResults.GetEnumerator();
while (featureEnumerator.MoveNext())
{
SPFeature feature = featureEnumerator.Current;
feature.Upgrade(false);
}
}

B.
SPWebServiceCollection webServices = new SPWebServiceCollection(SPFarm.Local);
foreach (SPWebService myWebService1 in webServices)
{
SPFeatureQueryResultCollection queryResults = myWebService1.QueryFeatures(SPFeatureScope.Web, true);
IEnumerator<SPFeature> featureEnumerator = queryResults.GetEnumerator();
while (featureEnumerator.MoveNext())
{
SPFeature feature = featureEnumerator.Current;
feature.Upgrade(false);
}
}

C.
SPSite site = SPContext.Current.Site;
SPFeatureCollection allFeatures = site.Features;
foreach (SPFeature currentFeature in allFeatures)
{
currentFeature.Upgrade(true);
}

D.
SPWeb web = SPContext.Current.Web;
SPFeatureCollection allFeatures = web.Features;
foreach (SPFeature currentFeature in allFeatures)
{
currentFeature.Upgrade(true);
}

Explanation:
MNEMONIC RULE: “large chunk of code, SPFeatureScope.Site”

Since we are working with the site collection, we need to use SPFeatureScope.Site, not SPFeatureScope.Web.

needsUpgrade (Boolean): if true, only features that need to be upgraded are included. If false, only features that do not need to be upgraded are included.

SPSite.QueryFeatures Method (Guid, Boolean)
http://msdn.microsoft.com/en-us/library/ee545763.aspx



Leave a Reply 0

Your email address will not be published. Required fields are marked *