1

I am following this tutorial and I am trying to setup the code in a Event Receiver.

I need 2 properties to send into their method a SPWeb and string.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    // is there a way to make this non hardcoded? 
    SPSite site = new SPSite("http://localhost.com");
    SPWeb web = site.OpenWeb("/");
    string XMlPath = // get xml file path
    CreateGroups(web, path);
}

private void CreateGroups(SPWeb currentSite, string groupsFilename)
{

}

So I tried to use getFullPath but that did not work. I also tried to use MapPath but I did not seem to have access to that.

So how do I get XML file (I think thats what I need)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • You need to dispose the `SPSite` and `SPWeb` objects - the easiest way to do it is the `using` statement. http://msdn.microsoft.com/en-us/library/yh598w02.aspx – Marek Grzenkowicz Apr 06 '11 at 20:04
  • @Marek Grzenkowicz - One thing at a time. I don't really care about disposing right now(I think with SPWeb doing that can cause problems too- I have to check in my code). I don't know what a 14 hive folder is. I don't think I have deployed the XML file as part of the feature. I just have a hardcoded path that gets the xml document. – chobo2 Apr 06 '11 at 21:55
  • Look for your feature folder in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\. Place the XML file in this folder and you will be able to access it without specifying the full path. – Marek Grzenkowicz Apr 06 '11 at 22:10
  • @Marek Grzenkowicz- What happens if I move this project some where else will that folder come with me? I am looking for some where I can have it in my solution and if I move the code to another computer I don't have to worry about missing files or changing paths. – chobo2 Apr 06 '11 at 22:19
  • Then add the XML file to your feature in Visual Studio. When you build and package it, the resulting WSP file will contain the XML file. Every time you deploy the feature (using the WSP file or directly from Visual Studio), the XML file will be placed in the *14 hive* folder. – Marek Grzenkowicz Apr 06 '11 at 22:26
  • @Marek Grzenkowicz - How do I add it to my feature. Right now it is in it's own folder called "XML". Can you maybe walk me through with some steps I am very new to sharepoint. – chobo2 Apr 06 '11 at 22:30
  • Right click on your feature node in Visual Studio (the one that contains `Elements.xml` inside) and choose option to add an existing file. Deploy the package to SharePoint and check if the XML file was deployed along with other feature files. – Marek Grzenkowicz Apr 07 '11 at 07:37

1 Answers1

5
  1. You need to dispose of the SPSite / SPWeb object, this is usually done in a using clause.
  2. You don't need to use an absolute path (hard code) in a feature receiver, as the feature is already web/site scoped
  3. your XmlPath usually needs to point to a file on the Sharepoint server, which you also deployed in your Feature - as the feature receiver is running after all the normal files have been deployed, you're good.

Without further ado, slightly different code:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    //Web scoped feature?
    //spWeb = (SPWeb) properties.Feature.Parent;
    //assuming Site scoped feature
    spWeb = ((SPSite) properties.Feature.Parent).RootWeb;

    using (spWeb)
    {
        string XmlPath = properties.Definition.RootDirectory + @"\Xmlfile\groups.xml"
        CreateGroups(spWeb, XmlPath);
    }
}

So how do you get your XML file into "\Xmlfile\groups.xml"? Just create a module! (Add new item > Module) The elements.xml of your module should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Name="Xmlfile" Path="Xmlfile">
        <File Path="groups.xml" Url="Xmlfile/groups.xml" />
    </Module>
</Elements>

Of course you will need to add your groups.xml file into that module (Context menu > Add existing item) for this to work.
Also note that you can easily debug feature receivers, just make sure that the deployment configuration is set to "No Activation" (Project Properties > Sharepoint > Active Deployment Configuration) - this way you will need to manually activate the feature on the site (instead of Visual Studio doing it automatically for you in debug mode) - but debugging will work flawlessly.

Dennis G
  • 21,405
  • 19
  • 96
  • 133
  • See this about disposing http://insidesharepoint.blogspot.com/2007/07/trying-to-use-closeddisposed-webpart.html . I keep getting this error when I tried to dispose SPSite site = SPContext.Current.Site; But maybe since I am creating a new site object it is different. I will try. – chobo2 Apr 07 '11 at 19:17
  • Do **not** create a new site object like you did in your code in a FeatureReceiver! See MS best practices: http://msdn.microsoft.com/en-us/library/ee724407.aspx – Dennis G Apr 07 '11 at 20:41
  • Love how the msdn follows the best practices...http://msdn.microsoft.com/en-us/library/ee231604.aspx#Y912 – chobo2 Apr 07 '11 at 20:53
  • So I make a model(Say I call it XMlfile). I then take my existing xml file(called permissions) and add it this module. then in the url change it to Xmlfile/permissions.xml? – chobo2 Apr 07 '11 at 21:32
  • @chobo2 You're right about MSDN inconsistencies ;-) And you are correct about the module. – Dennis G Apr 08 '11 at 08:27