If You have your feature as site than you do:
SPSite site = properties.Feature.Parent as SPSite;
if its web
SPWeb web = properties.Feature.Parent as SPWeb;
so for you, you should use spsite not web!:
[Guid("fce84747-959a-4ea2-bcda-265d3675d207")] public class LibrariesEventReceiver : SPFeatureReceiver { // Uncomment the method below to handle the event raised after a feature has been activated. public override void FeatureActivated(SPFeatureReceiverProperties properties) { try { SPSite rootSite = properties.Feature.Parent as SPSite; //AuthorizedUserGroup Guid authorizedUserFieldGUID = Guid.Parse("38900A0D-4BF4-434C-B130-12D959E56E1D"); SPField authorizedUserField = rootSite.RootWeb.Fields[authorizedUserFieldGUID]; Guid descriptionGUID = Guid.Parse("b22207cd-d6de-4f0b-bdc4-6ea8a5b37714"); //Description if(rootSite.RootWeb.Fields.Contains(descriptionGUID)) { SPField description = rootSite.RootWeb.Fields[descriptionGUID]; } } catch (Exception) { throw; } } }
above does work, added in a check to see if the field actually exist on the rootweb! looks like you must have the guid wrong or it doesnt exist! anyhoo it will work without throwing an error but if it fails the if function it will not set SPField description ! cant you use the string name rather than the guid?
EDIT
just like to make it clear, if you set your feature to web you need to use spweb and not spsite as it would be null and same goes oposite way round! You can use this code to do what you want:
SPWeb site; if (properties.Feature.Parent is SPWeb) { site = (SPWeb)properties.Feature.Parent; } else if (properties.Feature.Parent is SPSite) { site = ((SPSite)properties.Feature.Parent).RootWeb; } else { throw new Exception("Unable to get SPWeb object, this feature is not Site or Web-scoped."); }
First set your feature to site scope!!!! than do this code: its made for simplicity for you and should work!
so in your code:
[Guid("fce84747-959a-4ea2-bcda-265d3675d207")] public class LibrariesEventReceiver : SPFeatureReceiver { // Uncomment the method below to handle the event raised after a feature has been activated. public override void FeatureActivated(SPFeatureReceiverProperties properties) { try { SPWeb rootSite; if (properties.Feature.Parent is SPWeb) { rootSite = (SPWeb)properties.Feature.Parent; } else if (properties.Feature.Parent is SPSite) { rootSite = ((SPSite)properties.Feature.Parent).RootWeb; } else { throw new Exception("Unable to get SPWeb object, this feature is not Site or Web-scoped."); } //AuthorizedUserGroup Guid authorizedUserFieldGUID = Guid.Parse("38900A0D-4BF4-434C-B130-12D959E56E1D"); SPField authorizedUserField = rootSite.RootWeb.Fields[authorizedUserFieldGUID]; //Description Guid descriptionGUID = Guid.Parse("b22207cd-d6de-4f0b-bdc4-6ea8a5b37714"); SPField description = rootSite.RootWeb.Fields[descriptionGUID]; } catch (Exception) { throw; } } }