Sharepoint应用程序页面仅在对话框中抛出异常

时间:2011-05-02 10:17:01

标签: c# sharepoint dialog applicationpage

我在对话框中运行应用程序页面(/_layouts/Collages/UploadCollage.aspx)时遇到异常,其中嵌入了内联代码。

在default.aspx页面上运行对话框的代码:

<script type="text/javascript">

//Handle the DialogCallback callback 
function DialogCallback(dialogResult, returnValue)
{
     // Occurs when upload succeeds
     if(SP.UI.DialogResult.OK == dialogResult)
     {
        this.statusId = SP.UI.Status.addStatus("Publishing complete:", "Published content available on Collages Library", true);

        SP.UI.Status.setStatusPriColor(this.statusId, "green");
     }
     // Occurs when upload throw exception
     if(SP.UI.DialogResult.cancel == dialogResult)
     {    
        this.statusId = SP.UI.Status.addStatus("Publishing failure", "An error occurs. Try again", true);
        SP.UI.Status.setStatusPriColor(this.statusId, "red");
     }
} 

//Open the Dialog 
function OpenUploadDialog()
{ 
   SP.UI.Status.removeAllStatus(true);
   var dialogOptions = SP.UI.$create_DialogOptions();
   dialogOptions.width = 600;
   dialogOptions.height = 800;
   dialogOptions.url = "/_layouts/Collages/UploadCollage.aspx";
   dialogOptions.dialogReturnValueCallback = Function.createDelegate(
                    null, DialogCallback);
   SP.UI.ModalDialog.showModalDialog(dialogOptions);
}</script><p>?</p>
<p>Click here to add new collage:</p>
<a href="javascript:OpenUploadDialog()">Add new collage</a>

UploadCollage.aspx(仅包含内联代码的* .aspx文件):

<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" MasterPageFile="~/_layouts/v4.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

<script type="text/C#" runat="server">


        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void CreateFolder(string library, string title)
        {
            using (SPSite site = SPContext.Current.Site)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        SPList imagesLibrary = web.Lists[library];
                        SPListItem newFolder = imagesLibrary.Items.Add(imagesLibrary.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, title);
                        newFolder.Update();
                    }
                    catch (Exception)
                    {
                        // Close dialog - JS exception
                        Context.Response.Write("<script type='text/javascript'>window.frameElement.cancelPopUp();<"+"/script>");
                        Context.Response.Flush();
                        Context.Response.End();
                    }
                }
            }
        }
        public void Click_saveButton(object sender, EventArgs e)
        {
            foreach (IValidator val in Validators)
            {
                if (!val.IsValid)
                {
                    return;
                }
            }

            CreateFolder(libraryNameTextBox.Text, collageTitleTextBox.Text);

            UploadImage(libraryNameTextBox.Text, image1FileUpload.FileContent, collageTitleTextBox.Text,
                        Path.GetExtension(image1FileUpload.FileName), "a");
            UploadImage(libraryNameTextBox.Text, image2FileUpload.FileContent, collageTitleTextBox.Text,
                        Path.GetExtension(image2FileUpload.FileName), "b");
            UploadImage(libraryNameTextBox.Text, image3FileUpload.FileContent, collageTitleTextBox.Text,
                        Path.GetExtension(image3FileUpload.FileName), "c");
            UploadImage(libraryNameTextBox.Text, image4FileUpload.FileContent, collageTitleTextBox.Text,
                        Path.GetExtension(image4FileUpload.FileName), "d");

            // Close dialog - JS commiting
            Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();<"+"/script>");
            Context.Response.Flush();
            Context.Response.End();
        }

        public void UploadImage(string library, Stream stream, string folder, string extension, string file)
        {
            using (SPSite site = SPContext.Current.Site)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        SPList imagesLibrary = web.Lists[library];
                        SPFolder collageFolder = imagesLibrary.RootFolder.SubFolders[GetFolderID(imagesLibrary.RootFolder.SubFolders, folder)];
                        collageFolder.Files.Add(file + extension, stream);
                        collageFolder.Update();
                    }
                    catch (Exception ex)
                    {
                        // Close dialog - JS exception
                        Context.Response.Write("<script type='text/javascript'>window.frameElement.cancelPopUp();<"+"/script>");
                        Context.Response.Flush();
                        Context.Response.End();
                    }
                }
            }
        }

        private int GetFolderID(SPFolderCollection folders, string folder)
        {
            for (int i = 0; i < folders.Count - 1; i++)
            {
                if (folders[i].Item.DisplayName == folder)
                {
                    return i;
                }
            }
            return 999;
        }

</script>

<SharePoint:UIVersionedContent ID="UIVersionedContent1" UIVersion="4" runat="server">
  <contenttemplate>
      <SharePoint:CssRegistration Name="forms.css" runat="server"/>
  </contenttemplate>
</SharePoint:UIVersionedContent>

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

<asp:ScriptManagerProxy runat="server" ID="ProxyScriptManager">
</asp:ScriptManagerProxy>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
      <td valign="top">
          <table cellspacing="0" cellpadding="4" border="0" width="100%">
              <tr>
                  <td class="ms-vb">
                      &nbsp;
                    </td>
                </tr>
            </table>
            <table border="0" width="100%">
                <tr>
                    <td>
                        <table border="0" cellspacing="0" width="100%">
                            <tr>
                                <td class="ms-formlabel" valign="top" nowrap="true" width="25%">
                                    <b> Library Name:</b>
                                </td>
                                <td class="ms-formbody" valign="top" width="75%">
                                    <asp:TextBox ID="libraryNameTextBox" Text="Collages" runat="server" /><br />
                                    <asp:RequiredFieldValidator ID="libraryNameTextBoxRequiredFieldValidator"
                                                                ControlToValidate="libraryNameTextBox"
                                                                ErrorMessage="Pole nazwa bibioteki nie może być pusta"
                                                                runat="server" />
                                </td>
                            </tr>
                            <tr>
                                <td class="ms-formlabel" valign="top" nowrap="true" width="25%">
                                    <b> Collage Name:</b>
                                </td>
                                <td class="ms-formbody" valign="top" width="75%">
                                    <asp:TextBox ID="collageTitleTextBox" Text="Kolaż 1" runat="server" /><br />
                                    <asp:RequiredFieldValidator ID="collageTitleRequiredFieldValidator"
                                                                ControlToValidate="collageTitleTextBox"
                                                                ErrorMessage="Pole tytuł nie może być puste"
                                                                runat="server" />
                                </td>
                            </tr>
                            <tr>
                                <td width="25%" class="ms-formlabel">
                                    <b> Image A:</b>
                                </td>
                                <td width="75%" class="ms-formbody">
                                    <asp:FileUpload ID="image1FileUpload" runat="server" /><br />
                                    <asp:RequiredFieldValidator ID="image1FileUploadRequiredFieldValidator"
                                                                ControlToValidate="image1FileUpload"
                                                                ErrorMessage="Należy wskazać zdjęcie do załadowania"
                                                                runat="server" />
                                </td>
                            </tr>
                            <tr>
                                <td width="25%" class="ms-formlabel">
                                    <b> Image B:</b>
                                </td>
                                <td width="75%" class="ms-formbody">
                                    <asp:FileUpload ID="image2FileUpload" runat="server" /><br />
                                    <asp:RequiredFieldValidator ID="image2FileUploadRequiredFieldValidator"
                                                                ControlToValidate="image2FileUpload"
                                                                ErrorMessage="Należy wskazać zdjęcie do załadowania"
                                                                runat="server" />
                                </td>
                            </tr>
                            <tr>
                                <td width="25%" class="ms-formlabel">
                                    <b> Image C:</b>
                                </td>
                                <td width="75%" class="ms-formbody">
                                    <asp:FileUpload ID="image3FileUpload" runat="server" /><br />
                                    <asp:RequiredFieldValidator ID="image3FileUploadRequiredFieldValidator"
                                                                ControlToValidate="image3FileUpload"
                                                                ErrorMessage="Należy wskazać zdjęcie do załadowania"
                                                                runat="server" />
                                </td>
                            </tr>
                            <tr>
                                <td width="25%" class="ms-formlabel">
                                    <b> Image D:</b>
                                </td>
                                <td width="75%" class="ms-formbody">
                                    <asp:FileUpload ID="image4FileUpload" runat="server" /><br />
                                    <asp:RequiredFieldValidator ID="image4FileUploadRequiredFieldValidator"
                                                                ControlToValidate="image4FileUpload"
                                                                ErrorMessage="Należy wskazać zdjęcie do załadowania"
                                                                runat="server" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>


            <table cellspacing="0" cellpadding="4" border="0" width="100%">
                <tr>
                    <td nowrap="nowrap" class="ms-vb">
                        <asp:Button Text="Cancel" runat="server" ID="cancelButton" OnClientClick="window.frameElement.commitPopup();" />
                    </td>
                    <td nowrap="nowrap" class="ms-vb" width="99%">
                        <asp:Button Text="Publish" ID="saveButton" OnClick="Click_saveButton" runat="server" />
                    </td>
                </tr>
            </table>
        </td>
        <td width="1%" class="ms-vb" valign="top">&nbsp;</td>
    </tr>
</table>


</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Dodawanie zdjęć do kolażu
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
    Dodawanie zdjęć do kolażu
</asp:Content>

以上代码在Sharepoint对话框中运行时无法正常工作。它在没有对话框的情况下运行时有效。 (在网址:/_layouts/Collages/UploadCollage.aspx)。

任何有助于使其与对话框一起使用的帮助将非常感激。可能是问题的根源是什么?

1 个答案:

答案 0 :(得分:0)

问题回答。

我在C#代码中犯了错误:

private int GetFolderID(SPFolderCollection folders, string folder)
    {
        for (int i = 0; i < folders.Count - 1; i++)
        {
            if (folders[i].Item.DisplayName == folder)
            {
                return i;
            }
        }
        return 999;
    }

应该是:

private int GetFolderID(SPFolderCollection folders, string folder)
    {
        for (int i = 0; i < folders.Count; i++)
        {
            if (folders[i].Name == folder)
            {
                return i;
            }
        }
        return 999;
    }

经过更正,它应该可以完美地运作......