Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:48:54+00:00 2026-05-26T08:48:54+00:00

I’ve put together a function that creates a sharepoint folder in a document library

  • 0

I’ve put together a function that creates a sharepoint folder in a document library based on the url that’s past in as an argument. The code works and the folder shows up in sharepoint from the webapplication.

However, when I query the SPWeb object for the folder afterward, it says the folder doesnt exist. Which makes no sense to me. Stranger still, is that this very same code worked no too long ago. I had been using it to create tree structures in sharepoint.

Even if the query folder fails, the GetFolder still returns a the folder, but when I add files to the returned folder, I get a runtime exception indicating that the file doesn’t exist…which I assume means the folder I am trying to add it to doesn’t exist since the file I am adding, doesn’t exist yet. Which is why I am adding it.

So my question is, why am I getting this error, and why does FolderExists return false when the folder actually exists? We know it exists because GetFolder actually returns it…

I’ve included some actual code from the app to make things clear.

If someone could have a look at the code and see and anything jumps out at them, that would be fantabulous…Thanks

Code to build folders:

    public void CreateFolder(SPUriBuilder url)
    {
        try
        {
            Log.Instance.WriteToLog("CreateFolder({0})", url);

            var library = GetLibrary(url.Library);
            if (library != null)
            {                    
                //  parse out string data
                //                    
                var parent = library.RootFolder.ServerRelativeUrl;
                var segments = url.Account.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                var path = parent;

                //  get default folder collection
                //
                SPFolderCollection subFolders = _web.GetFolder(parent).SubFolders;

                //  check for sub-folders to create
                //
                if (segments.Length > 0)
                {
                    int i = 0;
                    do
                    {
                        //  check for folder and create if non-existant
                        //
                        var buildPath = String.Format("{0}/{1}", path, segments[i]);

                        if (_web.GetFolder(buildPath).Exists == false)
                            _web.GetFolder(path).SubFolders.Add(segments[i]);                            

                        //  retrieve new sub-folder collection
                        //
                        subFolders = _web.GetFolder(buildPath).SubFolders;                            
                        path = buildPath;

                        //  next folder in path
                        //
                        i++;
                    }
                    while (i < segments.Length);

                }

                //  finally, add folder of interest
                //
                subFolders.Add(url.Folder);                    

            }
        }
        catch (Exception e)
        {
            throw new SPImportException("Exception: {0}, creating folder: {1} in Library: {2}", e.Message, url.Folder, url.Library);
        }
    }

Code to Query folder:

    public bool FolderExists(SPUriBuilder url)
    {
        return _web.GetFolder(url.Uri.LocalPath).Exists;
    }

Code to Get Folder:

    private SPFolder GetFolder(SPUriBuilder url)
    {
        return _web.GetFolder(url.Uri.LocalPath);
    }

The SPUriBuilder is a custom class I created to assemble the Uri:

public class SPUriBuilder
{
    public string SiteUrl { get; private set; }        
    public string Library { get; private set; }
    public string Parent { get; private set; }
    public string Folder { get; private set; }
    public string File { get; private set; }
    public string Account { get; private set; }

    public Uri Uri { get; private set; }

    public SPUriBuilder(string siteUrl, string library, string account, string parent, string folder)
    {
        this.SiteUrl = siteUrl;
        this.Library = library;
        this.Account = account;
        this.Parent = parent.Replace("\\", "/");
        this.Parent = this.Parent.StartsWith("/") ? this.Parent.Substring(1) : this.Parent;
        this.Folder = folder;

        StringBuilder url = new StringBuilder();

        url.AppendFormat("{0}/{1}/{2}", SiteUrl, Library, Account);
        if (String.IsNullOrEmpty(Parent) == false)
            url.AppendFormat("/{0}", Parent);
        url.AppendFormat("/{0}", Folder);

        this.Uri = new Uri(url.ToString());

    }

    public SPUriBuilder(SPUriBuilder uri, string file)
        : this(uri.SiteUrl, uri.Library, uri.Account, uri.Parent, uri.Folder)
    {
        this.File = file;

        StringBuilder url = new StringBuilder();

        url.AppendFormat("{0}/{1}", this.Uri.ToString(), this.File);

        this.Uri = new Uri(url.ToString());

    }

    public override string ToString()
    {
        return Uri.ToString();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:48:54+00:00Added an answer on May 26, 2026 at 8:48 am

    I found the answer this to this myself. The problem was in the code used to create the folder.

          var parent = library.RootFolder.ServerRelativeUrl;
    
          //  This line of code is incorrect, so it returned the wrong data, thus building the folder path incorrectly.
          //
          var segments = url.Account.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
          var path = parent;
    
    
          //   This is the replacement line of code that fixed the issue.
          //
          var segments = url.Uri.LocalPath.Substring(parent.Length+1).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
    
        //   as well, this line had to be removed since it was no longer needed
        //
        //  finally, add folder of interest
                //
                subFolders.Add(url.Folder);  
    

    Ultimately the issue turned out be that the folder structure did not exist that I was attempting to create the file in. One or more segments in the path were missing.

    So if you ever see this error, make sure you’re the folder exists that you are adding the file to. If it isn’t, you will certainly experience this error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.