0

I am building my WebPart locally and when ready will deploy to SPO. I want to traverse the items of a list using LINQ (or whatever is most proper) and generate HTML based on those items.

That's all I want to do. That's all. I don't want to bind to a gridview or any other asp:control which I have no control on what it spits out and seems like an overkill to create my own (if that's even doable)

Why is this so hard to do ?

EDIT

Adding some more information to the question.

I have a list of projects (stored as list items with some properties and attachments) that need to be displayed through some custom HTML in the public facing site.

To be able to do this on my local environment I'm trying to build a simple web-part (based on this example http://msdn.microsoft.com/en-us/office365trainingcourse_lab_2_1_topic3 ) that does not need the button click.

It would obviously need the SPQuery code from that example (after some more research it seems that using LINQ on List Items is not as efficient as using SPQuery)

Here's what I have so far:

namespace ProjectsWebPart.ProjectsWebPart{
[ToolboxItemAttribute(false)]
public partial class ProjectsWebPart : WebPart{
    public ProjectsWebPart(){
    }

    protected override void OnInit(EventArgs e){
        base.OnInit(e);
        InitializeControl();
    }

    protected void Page_Load(object sender, EventArgs e){

    }

    protected override void Render(System.Web.UI.HtmlTextWriter writer){

        SPList sourceList = SPContext.Current.Web.Lists.TryGetList("Projects List");

        SPQuery qry = new SPQuery();
        qry.ViewFieldsOnly = true;
        qry.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Author' />";
        qry.RowLimit = 20;

        String html = "";

        foreach (SPListItem item in sourceList.GetItems(qry)){
            html += "<span>" + item["Title"] + "</span>";
        }

        writer.WriteEncodedText(html);
        base.Render(writer);
    }

}

}

Obviously WriteEncodedText will do what it says so I'm not really generating HTML. The ascx file is just the configs up top, after I removed the 3 asp:controls

I come from the functional (Scala-Play based) world where simplicity is king. Chasing simplicity, I might be missing something obvious, so please let me know what that is. However if what I am saying makes sense, how do I now echo the HTML that I have built ?

A few more things that I would need is to pass parameters to the web-part. Is there a simple way of doing this ? I might need to filter by Project Title.

Thanks for any help on this.

  • I think you need to extend this question with details about your approach etc. – Robert Lindgren May 17 '13 at 6:10
  • Sure, will add some more info. Has been very frustrating on how much setup is needed in Sharepoint for even very simple tasks. – haknick May 17 '13 at 7:41
1

Use asp:repeater. I know you didn't want to use asp:controls but that's the way to go. You have 100% control over what the output will be.

//Robert

  • Ahhh, this might work and seems nice and clean, and also not mixed up with logic as well. Will try and get back after getting some sleep. It's 4:22am here now. Would you also know of any way to pass parameters to the web-part ? – haknick May 17 '13 at 8:23
  • How about a querystring parameter? Request.QueryString["param"] – Sky May 17 '13 at 10:26
  • Thanks for the reply. I meant, parameters sent from the Site Page to the web-part so that the web-part can be reused in different contexts. For example, show 10 items in one page but 30 items on another, or show the featured items in one page, and all of them in another. Any clues ? – haknick May 17 '13 at 16:26
  • 1
    Use a custom property in the webpart. Then you can edit the web part for each place you have the web part added. – Sky May 20 '13 at 8:42
  • I am adding the webpart manually in the Web-Part Page like so: <WebPartPages:SPUserCodeWebPart runat="server" Title="PeopleWebPart - PeopleWebPart" AssemblyFullName="PeopleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ccb95db35b99d7b6" SolutionId="ea63f3dc-ab0a-4d5d-bdb8-c30c1c542adb" ID="g_5b7d2737_0733_441e_bbd5_08672344c6a6" TypeFullName="PeopleWebPart.PeopleWebPart.PeopleWebPart" __MarkupType="vsattributemarkup" __WebPartId="{5B7D2737-0733-441E-BBD5-08672344C6A6}" WebPart="true" ></WebPartPages:SPUserCodeWebPart> Is there a way to add it inside SPUserCodeWebPart? – haknick May 20 '13 at 16:58

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.