Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am creating the document library in the code behind, I want to display that library in the quick launch.

My code is:

bool flag=true;
protected void Page_Load(object sender, EventArgs e)
{
    lblMsg.Visible = false;
    txtDocumentLibrary.Focus();
    btnDocumentLibrary.Click += new EventHandler(btnDocumentLibrary_Click);
}

void btnDocumentLibrary_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(txtDocumentLibrary.Text))
    {
        lblMsg.Text = "Please Enter Name";
        lblMsg.ForeColor = System.Drawing.Color.Red;
        lblMsg.Visible = true;
    }
    else
    {             
          using(SPSite site = new SPSite(SPContext.Current.Site.ID))
          {
              using (SPWeb web = site.OpenWeb())
              {
                  // Chck if library name already exists...
                  foreach (SPList list in web.Lists)
                  {
                      if (list.Title.ToUpper().Equals(txtDocumentLibrary.Text.ToUpper()))
                      {
                           flag = false;
                           break;
                      }
                  }
                  if (flag)
                  {
                      web.AllowUnsafeUpdates = true;
                      Guid id = web.Lists.Add(txtDocumentLibrary.Text.Trim(), "Created Programmatically", SPListTemplateType.DocumentLibrary);
                       web.Update();
                       lblMsg.Text = txtDocumentLibrary.Text.Trim() + " Library Created Successfully";
                       lblMsg.ForeColor = System.Drawing.Color.Green;
                       lblMsg.Visible = true;
                       SPList lst = web.Lists[id];
                       lst.OnQuickLaunch = true;
                       lst.Update();
                       web.AllowUnsafeUpdates = false;
                   }
                   else
                   {
                       lblMsg.Text = "Library Name Already Exists Please Enter Another";
                       lblMsg.ForeColor = System.Drawing.Color.Red;
                       lblMsg.Visible = true;
                   }
               }
           }                     
     }
}

Everything works well but my problem is that the quick launch affects after the page is reloaded. i.e. if I want to see newly created library, I have to refresh the page. I want to update the quick launch the moment when the library is created.

How should I tackle this error?

share|improve this question

1 Answer 1

users1100,

In this situation where everything works right, I would probably just write in some code to perform the refresh for the user. I found a good source while searching this on the internet. Just create the method and call it after all of your other code has been executed.

Disclaimer: I have NOT tried this solution myself yet.

protected void ReloadPage()
{
 string script =
  @"<script language='JavaScript'>
  location.href=document.openlocation.href.value
  </script>";
 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), string.Empty, script, false);
}

The link to the site that explains this is at http://discoveringsharepoint.wordpress.com/2011/08/24/reload-page-from-code-behind/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.