1

How can I provide the following solution?

  • on the 'home page' of a site collection, query all lists with a certain content type ('eForms')
  • each list entry will be a link to the 'newform.aspx' of that particular list

I find that currently we are manually maintaining a list to all of our eForms which is becoming quite cumbersome. We do have the ability to apply content types to these lists so a CQWP or otherwise can 'latch onto' them and pull it. The problem is, I don't want the individual items within the list to appear on the CQWP, but rather need to just link to the "newform.aspx" so users can easily navigate to submit new form data.

Is this possible?

1 Answer 1

2
+50

Here's a solution in C# (assuming that you'll use the code in a visual webpart):

protected void Page_Load(object sender, EventArgs e)
    {
        var eForms = new List<EForm>();
        foreach (SPWeb web in SPContext.Current.Site.AllWebs)
        {
            var listsWithWantedContentType = web.Lists.Cast<SPList>()
                .Where(list => list.ContentTypes.Cast<SPContentType>()
                .Any(ct => ct.Name == "Item"));
            foreach (var list in listsWithWantedContentType)
            {
                eForms.Add(new EForm
                               {
                                   ListUrl = list.RootFolder.ServerRelativeUrl,
                                   ListTitle = list.Title,
                                   NewFormUrl = list.DefaultNewFormUrl
                               });                    
            }

            web.Dispose();
        }
        gvLists.DataSource = eForms;
        gvLists.DataBind();
    }

    class EForm
    {
        public string ListUrl { get; set; }
        public string ListTitle { get; set; }
        public string NewFormUrl { get; set; }
    }

gvList is a GridView control that you'll put inside the webpart ascx control.

<asp:GridView ID="gvLists" runat="server" AutoGenerateColumns="True"></asp:GridView>
6
  • 2
    I disagree. See here: sharepoint.stackexchange.com/questions/1021/…
    – MdMazzotti
    Mar 9, 2014 at 20:02
  • 1
    @MdMazzotti is correct, as he's not using the SPContext.Web but webs in the AllWebs collection, which need manual disposal. Mar 10, 2014 at 7:03
  • I have only dabbled in creating web parts in Visual Studio. How can I get this code to display the data it generates?
    – Kolten
    Mar 11, 2014 at 22:09
  • 1
    Updated the answer with an example of how to use it with a visual webpart
    – MdMazzotti
    Mar 12, 2014 at 10:40
  • hey could you possible help me out with a small problem? This line: var eForms = new List<EForm>(); Gives this error: The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?) I know it's probably a simple fix, and I am really showing my noobness with this, but could you direct me as to what may be wrong here?
    – Kolten
    Mar 21, 2014 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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