2

I am creating document library in code behind.
I added an application page and put one label, a text box and a button. Text box accepts the name of the document library, when button is pressed the library is created.

My code for creating library is :

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 = SPContext.Current.Site)
       {
           using (SPWeb web = site.RootWeb)
           {
               web.AllowUnsafeUpdates = true;
               Guid id = web.Lists.Add(txtDocumentLibrary.Text, "Created Programmatically", SPListTemplateType.DocumentLibrary);
               SPList lst = web.Lists[id];
               lst.OnQuickLaunch = true;
               web.Update();
               web.AllowUnsafeUpdates = false;
               lblMsg.Text = txtDocumentLibrary.Text + "Library Created Successfully";
               lblMsg.ForeColor = System.Drawing.Color.Green;
               lblMsg.Visible = true;
            }
        }
    }
}

First I used SPSite site = SPContext.Current.Site and used Dispose() method, and then I searched for resolving the error and found that the using() statement will be helpful, but after using such also throws error. How should I resolve it? Please help.

3

You should open new SPSite and SPWeb objects in the using statement, like:

using(SPSite site = new SPSite(SPContext.Current.Site.ID))
       {
           using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
           {
               web.AllowUnsafeUpdates = true;
               Guid id = web.Lists.Add(txtDocumentLibrary.Text, "Created Programmatically", SPListTemplateType.DocumentLibrary);
               SPList lst = web.Lists[id];
               lst.OnQuickLaunch = true;
               web.Update();
               web.AllowUnsafeUpdates = false;
               lblMsg.Text = txtDocumentLibrary.Text + "Library Created Successfully";
               lblMsg.ForeColor = System.Drawing.Color.Green;
               lblMsg.Visible = true;
            }
        }

Some info about disposing of SPWeb and SPSite objects:

  • You should never dispose the SPContext.Current.Web resp. SPContext.Current.Site, since these objects are currently used by SharePoint.
  • If you use "using" statements for opening fresh objects, you do not have to dispose the objects explicitly, when exiting the using block, dispose will be called automatically.
  • If not using "using" statements, you will in most cases have to call .Dispose() yourself, once you are done working with the object.

(more here, for wss 3.0 but all seems still valid)

Here are code examples for different patterns: http://msmvps.com/blogs/sundar_narasiman/archive/2011/07/30/sharepoint-2010-recommended-practices-for-disposing-objects-part1.aspx

  • Thank you sir, Its working perfectly. I want to know about the dispose() here we did not disposed the objects, will it not create any problem.. If any reference for when it is must to dispose the site and web objects, please provide if any.. @Robert Lindgren – users1100 Aug 13 '13 at 7:05
  • Updated with link, and some rule of thumb – Robert Lindgren Aug 13 '13 at 7:12
  • use "using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))" instead of "using (SPWeb web = site.OpenWeb())".. because if you use site.OpenWeb(), it will open SPWeb object of root site collection and if you are working with sub site it will not work.. – SID Aug 13 '16 at 10:25
0

in my strange case because i was just manipulating with SharePoint designer 2013, this error and exception is showing only to normal users and me as admin was not seeing that.

although i removed the 2 using in my weather webpart and fixed the problem but i think that this is not essential because this webpart was working before i removed many snippets of the page in SharePoint designer.

i am suspicious of the following code which is essentially a webpart zone title handler and is hidden but i think WebPartPages:TitleBarWebPart is responsible for fixing this problem .

note the "ms-hide" class

<div class="ms-hide">
    <WebPartPages:WebPartZone runat="server" title="loc:TitleBar" id="TitleBar" AllowLayoutChange="false" 
    AllowPersonalization="false" Style="display:none;"><ZoneTemplate>
    <WebPartPages:TitleBarWebPart runat="server" HeaderTitle="Home8" Title="Web Part Page Title Bar"
     FrameType="None" SuppressWebPartChrome="False" Description="" IsIncluded="True" 
     ZoneID="TitleBar" PartOrder="2" FrameState="Normal" AllowRemove="False" 
     AllowZoneChange="True" AllowMinimize="False" AllowConnect="True" AllowEdit="True" 
     AllowHide="True" IsVisible="True" DetailLink="" HelpLink="" HelpMode="Modeless" Dir="Default" 
     PartImageSmall="" MissingAssembly="Cannot import this Web Part." 
     PartImageLarge="" IsIncludedFilter="" ExportControlledProperties="True" 
     ConnectionID="00000000-0000-0000-0000-000000000000" ID="g_7fe5ac76_a5b6_4813_9238_fcbbcae38915" 
     AllowClose="False" ChromeType="None" ExportMode="All" 
     __MarkupType="vsattributemarkup" __WebPartId="{7FE5AC76-A5B6-4813-9238-FCBBCAE38915}" 
     WebPart="true" Height="" Width=""></WebPartPages:TitleBarWebPart>

    </ZoneTemplate></WebPartPages:WebPartZone>
  </div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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