Vybez
Vybez

Reputation:

How to get the URL for a SharePoint Discussion Board entry?

How do you retrieve the URL for a discussion board item? That is, the URL displayed when you mouse over the subject line (once the list has been added to the page as a web part).

Upvotes: 2

Views: 2620

Answers (7)

Vikas Bansal
Vikas Bansal

Reputation: 148

You can use use "TopicPageUrl" field column to directly get the discussion topic by using REST api URL

http://sp2013.in/_api/web/Lists/GetByTitle('Discussion')/Items?$select=Title,TopicPageUrl,DiscussionLastUpdated,Folder/ItemCount,LastReplyBy/Title,Author/Title&$expand=Folder,LastReplyBy,Author&$orderby=DiscussionLastUpdated desc

The above code is also useful to get discussion last updated, reply count (its saved in folder), last replied by.

Upvotes: 1

Aackose Lal
Aackose Lal

Reputation: 156

To generate the direct url to a particular discussion item, on Client side (using a REST API call), you could try this:

var jqXhr = $.ajax({
        url:"/DiscussionSite/_api/lists/getByTitle('Discussions')/items?
        $select=ID,FileRef,ContentTypeId,Title,Body&
        $filter=ContentType eq 'Discussion'",       
        headers: { 'Accept': 'application/json;odata=verbose'}
    });

// Fetch only the discussions from the Discussion list (excl. Messages)
jqXhr.done(function(data){
    // Picking only the first item for testing purpose
    // Feel free to loop through the response if necessary
    var firstItem = data.d.results[0],
    firstItemUrl = '/DiscussionSite/Lists/Discussions/Flat.aspx?RootFolder=' + firstItem.FileRef + '&FolderCTID' + firstItem.ContentTypeId;

    // Result - /DiscussionSite/Lists/Discussions/Flat.aspx?RootFolder=/DiscussionSite/Lists/Discussions/My Discussion Topic 1&FolderCTID0x01200200583C2BEAE375884G859D2C5A3D2A8C06
    // You can append "&IsDlg=1" to the Url for a popup friendly display of the Discussion Thread in a SharePoint Modal Dialog
    console.log(firstItemUrl);
});

Hope this helps!

Upvotes: 0

Kirk Liemohn
Kirk Liemohn

Reputation: 7933

You may not have the list item, but if you do just look at the "FileRef" property. It will look like "https://mycompany.sharepoint.com/sites/Lists/discussion/". If I put that URL in the browser (I'm using SharePoint Online), it redirects me to the https://mycompany.sharepoint.com/Lists/Discussion/Flat.aspx?RootFolder=... URL.

Upvotes: 0

yarik
yarik

Reputation: 21

protected void gvForum_RowDataBound(object sender, GridViewRowEventArgs e)
{
    SPListItem item = e.Row.DataItem as SPListItem;
    Label lblTitle = e.Row.FindControl("lblTitle") as Label;
    HtmlAnchor aURL = e.Row.FindControl("aURL") as HtmlAnchor;
    if (item != null)
    {
        if (lblTitle != null && aURL != null)
        {
            aURL.HRef = "~/" + item.Url;
            lblTitle.Text = item["Title"].ToString();                    
        }
    }
}

Upvotes: 1

yarik
yarik

Reputation: 21

    protected global::System.Web.UI.WebControls.GridView gvForum;
    public string Region
    {
        get
        {
            return "";
        }
    }
    public string DefaultRegion { get; set; }
    public int Top { get; set; }
    public string ListName
    {
        get
        {

            string listName=string.Empty;
            if (!string.IsNullOrEmpty(this.Region))
                listName=string.Format("{0} {1}","Forum",this.Region);
            else
                listName = string.Format("{0} {1}", "Forum", this.DefaultRegion);
            return listName;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    private void BindGrid()
    {
        string region = this.Region;
        string caml=@"<OrderBy><FieldRef Name=""Modified"" /></OrderBy>";
        try
        {
            using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb spWeb = spSite.OpenWeb())
                {
                    SPQuery spQ = new SPQuery();
                    spQ.Query = caml;
                    spQ.RowLimit = (uint)this.Top;
                    SPList spList = spWeb.Lists[ListName];
                    SPListItemCollection items = spList.GetItems(spQ);
                    if (items != null && items.Count > 0)
                    {
                        gvForum.DataSource = items;
                        gvForum.DataBind();
                    }
                    else
                    {
                        this.Visible = false;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Log(ex.Message, System.Diagnostics.EventLogEntryType.Error);
        }

    }
    protected void gvForum_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        SPListItem item = e.Row.DataItem as SPListItem;
        Label lblTitle = e.Row.FindControl("lblTitle") as Label;
        HtmlAnchor aURL = e.Row.FindControl("aURL") as HtmlAnchor;
        if (item != null)
        {
            if (lblTitle != null && aURL != null)
            {
                aURL.HRef = "~/" + item.Url;
                lblTitle.Text = item["Title"].ToString();


            }
        }
    }

Upvotes: 1

jain
jain

Reputation: 1

You can give just subject name like http://site/discussion/lists/discussionboard/discusontitlename or subject

Upvotes: 0

elorg
elorg

Reputation: 248

Are you asking how to find the URL for an individual discussion in a discussion board? Or an individual reply to a discussion?

Upvotes: 0

Related Questions