1

Yesterday I asked, where custom columns can come from. You helped me and told me that maybe it's event receiver. I created new event receiver and I added that try catch part:

(Of course I change ID's)

[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 web = properties.Feature.Parent as SPWeb;
                SPSite rootSite = web.Site;

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

However when I try deploy it, it fails:

Error occurred in deployment step 'Activate Features': Object reference not set to an instance of an object.

I have set features Scope: Web and I tried also Scope: Site, but I got same error message.

I comment out code and it seems that SPSite rootSite = web.Site; causes problem.

I used google last night and I didn't find answer. How I can fix this?

Sorry, that I need to ask help again here. I promise that this is not habit. :/

  • 2
    Put a break point at first line where you get the web object. and check whether web object is coming up properly. – Nadeem Yousuf Feb 21 '14 at 8:28
1

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;
        }
    }
}
  • OP already mentions that the scope is Web and is using correct code to get web reference in that case. – Nadeem Yousuf Feb 21 '14 at 9:09
  • no he said that he tried spweb and spsite, as he is using web he is than trying to use the site object, im proposing to have the feature as site and use the site object as that should work! – Ali Jafer Feb 21 '14 at 9:22
  • 2
    That won't make difference as the problem seems to be something else. If the scope is Web then getting web object using his code is correct. – Nadeem Yousuf Feb 21 '14 at 9:40
  • I agree with @NadeemYousuf – eirikb Feb 21 '14 at 9:43
  • If I set SPWeb rootSite = null; Visual Studio tells me that: Microsoft.SharePoint.SPWeb does not contain a definition for 'RootWeb' and no extension method 'RootWeb' accepting a fist argument of type... – user3172297 Feb 21 '14 at 12:48
0

If you continue to use a feature that is scoped Web, you can get to the RootWeb by doing:

                SPWeb web = properties.Feature.Parent as SPWeb;
                using (SPSite site = new SPSite(web.Site.ID))
                {
                    using (SPWeb rootWeb = site.OpenWeb(site.RootWeb.ID))
                    {
                        //do whatever here
                    }
                 }
  • If web was null in OPs code, wouldn't this code have the same problem when calling web.Site? – eirikb Feb 21 '14 at 9:35
  • Why would web be null if the feature is set to Web? – Falak Mahmood Feb 21 '14 at 12:36
  • This fails, same error message: "Error occurred in deployment step 'Activate Features': Object reference not set to an instance of an object." – user3172297 Feb 21 '14 at 12:53
  • Can you create a new Web scope feature and add a feature receiver to it? It's seems that something getting messed up with your assemblies. Also make sure you restart the Visual Studio, IISreset and timer job service. – Falak Mahmood Feb 21 '14 at 13:09
  • Thank you for your help! It seems that my Visual Studio project was messed up! I tested your solutions and those solutions work. However, solution which is in my first post worked also. – user3172297 Feb 24 '14 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.