locked
Help: Cannot apply indexing with [] to an expression of type 'Microsoft.Sharepoint.SPWeb' RRS feed

  • Question

  • Hello everyone,

    I'm getting the following error in the code below, can anyone please help me resolve this, I have searched the web and tried many things like changing the brackets and adding other lines of code, but they either fix this and give me another error or do nothing at all. Your help is greatly appreciated.

    Cannot apply indexing with [] to an expression of type 'Microsoft.Sharepoint.SPWeb'


    Code------

    using System;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Serialization;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;

    namespace HandBookRedirect
    {
        [Guid("88d94c82-c305-4f0b-8a4b-8c88d44de3b8")]

        public class HandBookRedirect : System.Web.UI.WebControls.WebParts.WebPart


        {
            public HandBookRedirect()
            {
            }


            protected override void CreateChildControls()
           
            {

                base.CreateChildControls();

                string caml = "<Where><Eq><FieldRef Name='Author'/><Value Type='Integer'><UserID Type='Integer'/></Value></Eq></Where>";

                SPQuery query = new SPQuery();
                query.Query = caml;

                SPWeb currentWeb = SPContext.Current.Web;
                SPList list = currentWeb["http://giomoss/handbook/"];


                SPListItemCollection results = list.GetItems(query);

                if (results.Count == 0)
                {
                    Page.Response.Redirect(list.DefaultViewUrl);
                }

            }
        }
     
    }
    Thursday, June 4, 2009 9:08 PM

Answers

  • The SPWeb class doesn't provide an indexer.  There's no member named "Item", which would be needed to use an indexer like you're trying to.  Try currentWeb.GetList("http://giomoss/handbook/"); instead.


    David Morton - http://blog.davemorton.net/ - @davidmmorton
    • Proposed as answer by Andrew Woodward Thursday, June 4, 2009 10:22 PM
    • Marked as answer by Harry Zhu Thursday, June 11, 2009 7:38 AM
    Thursday, June 4, 2009 9:13 PM
  • David is totally correct,  if you have other SharePoint specific question you could also take alook at these forums

    http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/threads

    www.21apps.com
    • Marked as answer by Harry Zhu Thursday, June 11, 2009 7:38 AM
    Thursday, June 4, 2009 10:22 PM
  • Hux,

    Check out this MSDN article on creating a web part,  it covers the SafeControls entry.

    http://msdn.microsoft.com/en-us/library/ms452873.aspx

    Andrew
    www.21apps.com
    • Marked as answer by Harry Zhu Thursday, June 11, 2009 7:38 AM
    Friday, June 5, 2009 8:26 PM

All replies

  • The SPWeb class doesn't provide an indexer.  There's no member named "Item", which would be needed to use an indexer like you're trying to.  Try currentWeb.GetList("http://giomoss/handbook/"); instead.


    David Morton - http://blog.davemorton.net/ - @davidmmorton
    • Proposed as answer by Andrew Woodward Thursday, June 4, 2009 10:22 PM
    • Marked as answer by Harry Zhu Thursday, June 11, 2009 7:38 AM
    Thursday, June 4, 2009 9:13 PM
  • David is totally correct,  if you have other SharePoint specific question you could also take alook at these forums

    http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/threads

    www.21apps.com
    • Marked as answer by Harry Zhu Thursday, June 11, 2009 7:38 AM
    Thursday, June 4, 2009 10:22 PM
  • Thank you guys, that worked great and I was able to build my solution. However my web part is not working, I'm getting the following error.

    Web Part Error: A Web Part or Web Form Control on this Page cannot be displayed or imported. The type could not be found or it is not registered as safe.

    I will post this in the Sharepoint Development forum as suggested by Andrew, but it would be great to hear back from you if you have any ideas.

    Thanks again!!!

    Hux
    Friday, June 5, 2009 3:53 PM
  • Hux,

    Check out this MSDN article on creating a web part,  it covers the SafeControls entry.

    http://msdn.microsoft.com/en-us/library/ms452873.aspx

    Andrew
    www.21apps.com
    • Marked as answer by Harry Zhu Thursday, June 11, 2009 7:38 AM
    Friday, June 5, 2009 8:26 PM