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;
            }
        }

No comments:

Post a Comment

Featured Post

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management https://docs.microsoft.com/en-us/azure/api-management/api-...

Popular posts