Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Set value in NewForm.aspx page

$
0
0

I have created custom field, which upload document in document library and store uploaded URL in comments(multi-line) column while click on Upload button.

This custom field plug-n-play which associate with any SP list.



This solution perfectly work with SP 2007(MOSS) site.


Problem : This solution not work in SP 2013/2016. See below code for insert link in comments(multi-line) column.

private void InsertLink(SPWeb web, string filename, SPList library)
        {
            try
            {
                string fieldTitle = _FieldToInsertLink.Title;
                Helper.WriteUlsVerbose(string.Format("Inserting link into field '{0}'", fieldTitle));
                string path = string.Format("{0}/{1}/{2}", web.ServerRelativeUrl != "/" ? web.ServerRelativeUrl : string.Empty, library.RootFolder.Url, filename);
                string urlToInsert = library.BaseTemplate == SPListTemplateType.PictureLibrary ?
                     string.Format("<img src=\"{0}\" border=\"0\" alt=\"{1}\">", path, filename) :
                     string.Format("<a href=\"{0}\">{1}</a>", path, filename);
                Helper.WriteUlsVerbose("UrlToInser: " + urlToInsert);


                // create javascript
                // if the existing text contains a <HR>, it is most likely a reply. so add the picture at the beginning instead of the end
                var js = new StringBuilder("\r\n");
                js.Append(Helper.JavaScriptGetTagFromIdentifierAndTitle());
                js.AppendFormat("var field = getTagFromIdentifierAndTitle('textarea','TextField','{0}');\r\n", fieldTitle);                
                js.Append("if (field != null)\r\n");
                js.Append("{\r\n");
                js.Append("if (field.value.length > 6)\r\n");
                js.Append("{\r\n");
                SPList list = SPContext.Current.List;
                if (list != null && list.BaseTemplate == SPListTemplateType.DiscussionBoard)
                {
                    js.Append("     if (field.value.contains('<HR>'))\r\n");
                    js.Append("     {\r\n");
                    js.Append("         field.value = field.value.substr(5);\r\n");
                    js.AppendFormat("         field.value = '<DIV>' + '{0}' + field.value;\r\n", urlToInsert);
                    js.Append("     }\r\n");
                    js.Append("     else\r\n");
                    js.Append("     {\r\n");
                    js.AppendFormat("field.value = field.value.substr(0, field.value.length-6) + '{0}' + '</DIV>';\r\n", urlToInsert);
                    js.Append("{\r\n");

                }
                else
                {
                    js.AppendFormat("     field.value = field.value.substr(0, field.value.length-6) + '{0}' + '</DIV>';\r\n", urlToInsert);
                }
                js.Append("}\r\n");
                js.Append("else if (field.value.length == 0)\r\n");
                js.Append("{\r\n");
                js.AppendFormat("     field.value = '<DIV>' + '{0}' + '</DIV>';\r\n", urlToInsert);
                js.Append("}\r\n");
                js.Append("}\r\n");

                // add javascript to page
                Helper.WriteUlsVerbose("Registering JavaScript");
                ScriptManager.RegisterStartupScript(this, typeof(RHUploadAndInsert), "setValue", js.ToString(), true);
            }
            catch (Exception ex)
            {
                HandleException(string.Format("Error in InsertLink: {0}", ex));
            }
        }
	}
}

Actually, I am getting problem from below line. Because SP list columns(NewForm.aspx) not rendering/hide in View Page Source in the browser. While these column render in SP 2007.

js.AppendFormat("var field = getTagFromIdentifierAndTitle('textarea','TextField','{0}');\r\n", fieldTitle); 

Due to columns not render in View Page Source in the browser So, This field return null.


I need field ID from View Page Source or any other alternative approach where I can store uploaded document URL.
Please review the code and suggest your view. I am also open for any other alternative approach.

Thanks,


Mukesh Ajmera|LinkedIn |http://mukeshajmera.wordpress.com/|

If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>