I am new to SharePoint development and recently get stucked by a problem of deploying own Sandboxed Solution that do a event receiver job (ItemAdded).

It create a subfolders in top folder in a library when top folder being created. I tested wsp in development environment and that worked as i expect, but after i upload it to Solutions of production site and activate it there is no effect.

What am I doing wrong?

Code:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EventReceiverProject5.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           using (SPSite site = properties.OpenSite())
           {
               using (SPWeb web = site.OpenWeb())
               {
                   base.ItemAdded(properties);
                   {
                       SPList list = web.Lists.TryGetList("Portfolio");

                       SPListItem projectFolder = properties.ListItem;

                       SPFolderCollection folders = list.RootFolder.SubFolders;
                       SPFolder a1 = folders.Add("/DocLib2" + "/" + projectFolder.Name + "/" + "01. About");
                       SPFolder a2 = folders.Add("/DocLib2" + "/" + projectFolder.Name + "/" + "02. Presentation");
                   }
               }
           }
       }


    }
}

Update: There is one thing i've found: Solution worked well on Production main site but not on any subsites.

I don't think you are allowed to use Microsoft.SharePoint.Workflow assembly in the sandbox environment. If you reference assemblies but don't use them they will still cause problems.

In sandbox solutions, you are free to use the following: All of Microsoft.SharePoint, except

SPSite constructor
SPSecurity
SPWorkItem and SPWorkItemCollection
SPAlertCollection.Add
SPAlertTemplateCollection.Add
SPUserSolution and SPUserSolutionCollection
SPTransformUtilities
Microsoft.SharePoint.Navigation
Microsoft.SharePoint.Utilities, except SPUtility.SendEmail
SPUtility.GetNTFullNameandEmailFromLogin
Microsoft.SharePoint.Workflow
Microsoft.SharePoint.WebPartPages, except SPWebPartManager
SPWebPartConnection
WebPartZone
WebPartPage
ToolPane
ToolPart

  • Ian, thank you for suggestion. I tried to remove all not necessary assemblies but it did not help. – Andrey Silaev Jun 4 '13 at 9:42
  • What assemblies are you left with ? Only partially trusted callers will be allowed which is pretty restricted in what it will let you do. – Ian Jun 4 '13 at 16:21
  • I left these: using System; using System.Security.Permissions; using Microsoft.SharePoint; – Andrey Silaev Jun 4 '13 at 19:42

Please take note that event receiver needs to be attached in a list to make it work. If your event receiver has ListUrl parameter then your solution would work but if doesn't i suggest to create a feature receiver within your project. and in this feature receiver force the attach of the event receiver by programmatically attaching the event receiver into your list

  • Ranjoe, sorry for maybe stupid question, but why then same Solution worked perfectly on Test Site in Dev Environment? – Andrey Silaev Jun 6 '13 at 17:39
  • Hi, maybe you are deploying using visual studio. remember when you create a project you sometimes selecting a specific list to test your solution. So in that case if you right click and deploy it is most probably automatically attach to your list. disclaimer - I am not seeing your solution therefore I'm just stating some possibility – Ranjoe Santos Jun 7 '13 at 4:38
up vote 0 down vote accepted

Finally, i get this work. At the beginning i didn't specify correct web url for SPFolderCollection.Add method. After adding properties.WebUrl to it, it getting work right.

Anyway thanks all for help.

Working code looks like that:

using System;
using Microsoft.SharePoint;

namespace DesignPortfolioProjectFolders.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
    /// <summary>
    /// An item was added.
    /// </summary>
    public override void ItemAdded(SPItemEventProperties properties)
    {
            base.ItemAdded(properties);
            {
                    using (SPWeb web = properties.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("Portfolio");

                        SPListItem projectFolder = properties.ListItem;

                        SPFolderCollection folders = list.RootFolder.SubFolders;
                        SPFolder a1 = folders.Add(properties.WebUrl + "/DocLib2" + "/" + projectFolder.Name + "/" + "01. About");
                        SPFolder a2 = folders.Add(properties.WebUrl + "/DocLib2" + "/" + projectFolder.Name + "/" + "02. Presentation");
                        }
                }
        }

    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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