Friday, January 9, 2015

Adding list items to a folder with in the list, SharePoint add item to custom list results in “Invalid URL value. A URL field contains invalid data. Please check the value and try again”

protected void submit_click(object sender, EventArgs e)
        {
            using (SPSite site = SPContext.Current.Site)
            {
                SPWeb web = site.OpenWeb();
                web.AllowUnsafeUpdates = true;

                bool isFolderExist = false;
                DateTime dt = DateTime.Now;
                SPListItem folderColl_item;
                SPListItemCollection folderColl;

                SPList list = web.Lists["ListName"];

                folderColl = list.Folders;
                foreach (SPListItem splistitem in list.Folders)
                {
                    SPFolder folder = splistitem.Folder;
                    if (folder.Name.Equals(dt.Year.ToString()))
                    {
                        isFolderExist = true;
                    }
                }

                if (!isFolderExist)
                {
                    folderColl_item = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, dt.Year.ToString());
                    folderColl_item.Update();
                    list.Update();
                }

                folderColl = list.Folders;
                foreach (SPListItem splistitem in list.Folders)
                {
                    SPFolder folder = splistitem.Folder;
                    if (folder.Name.Equals(dt.Year.ToString()))
                    {
                        SPListItemCollection itemColl = list.Items;
                        SPListItem item = itemColl.Add(folder.ServerRelativeUrl, SPFileSystemObjectType.File, null);

                        item["test"] = "test";
                        item.Update();
                        list.Update();
                    }
                }

                web.AllowUnsafeUpdates = false;
            }
        }

1 comment:

Featured Post

Building Secure APIs with FastAPI and Azure AD Authentication

Building Secure APIs with FastAPI and Azure AD Authentication Published on September 2, 2025 In today's world of microservices and API-f...

Popular posts