Wednesday, September 24, 2014

When Environment.Newline and “\r\n” fails to create a new line.



I had a unique situation where I needed to display some document links and I tried using both Environment.Newline and “\r\n” and none of these gave me the result I needed. Here is the situation below.

Document 1 = “Hello1.docx”
Document2 = “Hello2.pptx”
Document3 = “Hello3.xls”

All three documents reside in a document library with the following links

Document1 = https://YourServerName/SharedLibrary/ Hello1.docx
Document1 = https://YourServerName/SharedLibrary/ Hello2.pptx
Document1 = https://YourServerName/SharedLibrary/ Hello3.xls

Here is my html code to display them.
outHtml1 = @”<a class='static menu-item'target='_blank' href='https://YourServerName/SharedLibrary/Hello.docx'><span class='additional-background'><span class='menu-item-text'>Hello1 Doc</span></span></a><br />” + “\r\n”;

outHtml2 = @”<a class='static menu-item'target='_blank' href='https://YourServerName/SharedLibrary/Hello2.pptx'><span class='additional-background'><span class='menu-item-text'>Hello2 pptxc</span></span></a><br />” + + “\r\n”;

outHtml3 = @”<<a class='static menu-item'target='_blank' href='https://YourServerName/SharedLibrary/Hello3.xls'><span class='additional-background'><span class='menu-item-text'>Hello3 xls</span></span></a><br />” + “\r\n”;




        /// <summary>
        /// Get Aritifacts by GroupID
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        ///
        private bool GetArtifactsByGroup(SPList list, string ValidationProcedure, string Classification, string Group, out string strNavigationURL, string webAbsoluteUrl, out string strGroups)
        {
            bool blnFound = false;
            string outNavUrl = string.Empty;
            string strGroupList = string.Empty;
            string outHtml = string.Empty;
            StringBuilder sb = new StringBuilder();
            try
            {

                SPSecurity.RunWithElevatedPrivileges(
                  delegate()
                  {

                      SPListItemCollection myList = null;
                      SPQuery qryGetArtifactsByGroup = new SPQuery();
                      qryGetArtifactsByGroup.ViewAttributes = "Scope=\"FilesOnly\"";
                      qryGetArtifactsByGroup.Query = string.Concat("<Where>",
                       "<And>",
                           "<And>",
                              "<Eq>",
                                 "<FieldRef Name='Validation_x0020_Procedure' />",
                                 "<Value Type='Lookup'>" + ValidationProcedure + "</Value>",
                                "</Eq>",
                              "<Eq>",
                                 "<FieldRef Name='Classification' />",
                                 "<Value Type='Choice'>" + Classification + "</Value>",
                             "</Eq>",
                           "</And>",
                           "<Eq>",
                              "<FieldRef Name='Group' />",
                              "<Value Type='Lookup'>" + Group + "</Value>",
                           "</Eq>",
                        "</And>",
                     "</Where>");

                      qryGetArtifactsByGroup.ViewAttributes = "Scope = 'Recursive'";
                      qryGetArtifactsByGroup.ViewFields = @"<FieldRef Name='Title' />
                                                                  <FieldRef Name='Name' />
                                                                  <FieldRef Name='Group' />
                                                                  <FieldRef Name='Validation_x0020_Procedure' />
                                                                  <FieldRef Name='Classification' />
                                                                  <FieldRef Name='FileRef' />
                                                                  <FieldRef Name='ContentType' />
                                                                  <FieldRef Name='DocumentID' />
                                                                  <FieldRef Name='Group_x003a_Groups' />
                                                                  <FieldRef Name='Group_x003a_ID' />
                                                                  <FieldRef Name='GroupID' />";
                      myList = list.GetItems(qryGetArtifactsByGroup);

                      if (myList != null && myList.Count > 0)
                      {
                          foreach (SPListItem item in myList)
                          {
                              strGroupList = string.Concat(TranslateLookup(item, "Group").Trim(), "\r\n");
                              outNavUrl = webAbsoluteUrl + string.Format("/{0}", SPEncode.UrlDecodeAsUrl(item.Url)).Trim();
                              outHtml += @"<a class='static menu-item' target='_blank'  href='" + outNavUrl + "'><span class='additional-background'><span class='menu-item-text'>" + item.Name + "</span></span></a>" + "\r\n";
                              sb.AppendLine("<a class='static menu-item'  target='_blank' href='" + outNavUrl + "'><span class='additional-background'><span class='menu-item-text'>" + item.Name + "</span></span></a>" + "<br />");
                              blnFound = true;
                          }

                      }
                  });

            }
            catch (Exception err)
            {
                DispayErrormessage(err);
            }
           
            strNavigationURL = sb.ToString();
            strGroups = strGroupList;
            return blnFound;
        }


From the code above, the outHtml did not provide the needed line break.
The StringBuilder, Sb.AppenLine gave me the needed linebreak. I hope this may help someone out there with the same issue.
This should have worked. But, it did not work for me. The actual result is shown below:

No comments:

Post a Comment