Ask a questionAsk a question
 

QuestionCreate an internal subfolder of a task list

  • Wednesday, October 28, 2009 3:20 PMdseve Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi All, I have tried the following but I receive a "cannot create folder" error. SPFolder mysubFolder= web.Folders["Lists"].SubFolders["Tasks"]; SPSecurity.RunWithElevatedPrivileges(delegate() { mysubFolder.SubFolders.Add("FolderName"); mysubFolder.Update(); });

All Replies

  • Wednesday, October 28, 2009 3:16 PMdseve Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi All, I have tried the following but I receive a "cannot create folder" error. SPFolder mysubFolder= web.Folders["Lists"].SubFolders["Tasks"]; SPSecurity.RunWithElevatedPrivileges(delegate() { mysubFolder.SubFolders.Add("FolderName"); mysubFolder.Update(); });
  • Wednesday, October 28, 2009 3:33 PMGopinath Devadass Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Howdy,

    Make sure you written your code like below. The key thing is you need to instantiate your SPSite object inside the RunWithElevatedProivillages.

    Example:

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
    
        SPWeb web = new SPSite("http://SiteURL").OpenWeb();
    
        using (web)
        {
            SPList list = web.GetList(web.Url + "/Lists/Tasks");
            list.Folders.Add("MyFolder", SPFileSystemObjectType.Folder);
        }
    });
    
    Thanks,
    Gopinath Devadass - http://techblooms.com
  • Wednesday, October 28, 2009 3:40 PMMike Walsh MVPMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Is this question connected with Visual Studio Beta 2 ?


    FAQ sites: (SP 2010) http://wssv4faq.mindsharp.com; (v3) http://wssv3faq.mindsharp.com and (WSS 2.0) http://wssv2faq.mindsharp.com
    Complete Book Lists (incl. foreign language) on each site.
  • Wednesday, October 28, 2009 3:45 PMMarkus I_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Dseve


    I'd try it this way


    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
       using (SPSite site = new SPSite(SPContext.Current.Site.ID) {
         using (SPWeb modifyweb = site.AllWebs[websname])
         {
        SPList tasks = modfiyweb.Lists["Tasks"];
        SPListItem newFolder = tasks.Items.Add( "", SPFileSystemObjectType.Folder, folderName);
        newFolder.Update();


         }
       }

    ]);


    using a web reference inside a delegate that has been created outside is a bad idea. And don't forget to call Update() on the new folder.

    • Edited byMarkus I_ Wednesday, October 28, 2009 4:33 PM
    •  
  • Wednesday, October 28, 2009 4:18 PMPeter Holpar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Markus,

    That may work, but - just to add my two cents - I usually suggest to get the site ID outside the elevation block, like this:

    Guid siteID = SPContext.Current.Site.ID;
    or if the code runs outside of SharePoint content and you have an SPSite reference in site variable:
    Guid siteID = site.ID;

    and then

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
       using (SPSite site = new SPSite(siteID) {
         using (SPWeb modifyweb = site.AllWebs[websname])
         {
           SPList tasks = modfiyweb.Lists["Tasks"];
           SPListItem newFolder = tasks.Items.Add( "", SPFileSystemObjectType.Folder, folderName);
           newFolder.Update();
         }
       }
    ]);

    Peter

  • Wednesday, October 28, 2009 4:24 PMGopinath Devadass Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Peter,

    I would like to ask one question to you. What is the difference between open a site by ID and URL.

    Do you know any article explain about SharePoint internals archicture, I mean what is internaly happening when we instantiate a SPSite object.


    Thanks,

    Gopinath Devadass - http://techblooms.com
  • Wednesday, October 28, 2009 4:26 PMdioulos Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks is just fantastic guys. I actually learning something new. Do you think I can extend that to create a subfolder to the Attachment subfolder of the task list? Thanks
  • Wednesday, October 28, 2009 4:32 PMMarkus I_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Markus,

    That may work, but - just to add my two cents - I usually suggest to get the site ID outside the elevation block, like this:

    Guid siteID = SPContext.Current.Site.ID;
    or if the code runs outside of SharePoint content and you have an SPSite reference in site variable:
    Guid siteID = site.ID;



    Peter


    OK, Peter, I have to admit that I was lazy ;o)

    using SPContent.Current.User inside the delegate is very, very dangerous as this context has bben changed but not so with the Site.ID.

    But you are in danger of getting used to a bad habit (using SPContext inside the delegate), so I'm 100% with you.
  • Wednesday, October 28, 2009 5:02 PMPeter Holpar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi dioulos,

    "Do you think I can extend that to create a subfolder to the Attachment subfolder of the task list?"
    I don't recommend that. AFAIK the folder for the Attachment is NOT an SPFolder. It is a "real" folder. SPFolder is basically a list item having the specific Folder content type (or derived from that content type). For example, you won't get attachement folders when calling SPList.RootFolder.Folders on a list instance.

    Why would you need that?

    Gopinath,

    I think there is not too much difference, but you can check it using Reflector. I'm sure that approach is better than any articles you can find on the web. I've started to look that, but there is a lot of calls between different constructor overloads and some obfuscated code as well. I may have time to play with that later...

    Generally I use the constructor that accepts the URL as we usually knows the URL, but I think that might consume a bit more resource, as the URL must be first resolved to the Guid. It is usually not an issue, but you might have problems with that if you have alternate access mappings enabled, and call the constructor with a non-AAM URL.
    See comment from Rodney Viana (MSFT) at http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.aspx

    Peter
  • Wednesday, October 28, 2009 5:15 PMdioulos Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    My real issue is that I am developing a WF (Windows Workflow) which create a Task (A). Then the user can edit that task (A) and attach a file to it and then submit. All the relevant metadata (like title, assignto, etc) would be use to create another task (B) with the attachment. However the issue is that the offered visual element (CreateTask Activity) does not create the task until it dehydrate (i.e the next activity is an OnChangeTask activity, a delay/OnTaskCreated) and thus I cannot attach a files on a task that does not exist yet! By playing around ,with the sharepoint designer, I notice that you can create a folder with the item id as its name and it will somehow associate the task to the folder. The Delay and OnTaskCreate activity have has bug and does not work correctly that why I cannot used them. So my only solution is to create the attachment folder myself, hence the question. I will put the code into a Code Activity.
  • Wednesday, November 04, 2009 3:17 AMAaron Han - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

       Merge as the same thread.

    Best Regards,
    -Aaron