Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I would like a script for Refreshing a webpart when a new item is added to te list. Can anyone help me with this?

Kind regards, Vincent van den Eijnde SharepointServices.nl

share|improve this question
add comment

1 Answer

The XsltListViewWebPart, which is one of the default web parts you can drag onto a form in SharePoint Designer, has an "Auto Refresh" and a "ManualRefresh" property which may get you what you want. Read further below for a script that addresses your question specifically.

FWIW, if you create it programatically, I just happen to have a handy snippet:

        Dim wp3 As New XsltListViewWebPart()
        wp3.Description = "Links related to specific log items in blah blah"
        wp3.AllowClose = False
        wp3.ViewFlag = "8388621"
        wp3.AllowConnect = True
        wp3.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal
        wp3.TitleUrl = String.Format("/Lists/{0}", m_linksListName)
        wp3.AutoRefresh = False
        wp3.ManualRefresh = True 'causes refresh button to show
        wp3.AutoRefreshInterval = 15
        wp3.ExportMode = System.Web.UI.WebControls.WebParts.WebPartExportMode.All
        wp3.CatalogIconImageUrl = "/_layouts/images/itgen.png"
        wp3.ViewGuid = view2.ID.ToString()
        wp3.XmlDefinition = view2.GetViewXml()
        wp3.ParameterBindings = "<ParameterBinding Name=""Param1"" & _
                                 " Location=""QueryString(ID)"" DefaultValue=""""/> "
        wp3.ListId = m_linksList.ID
        wp3.ListName = m_linksList.ID.ToString

You'll notice it can set to AutoRefresh, or you can cause a little recycle icon to render if you set ManualRefresh to true.


Now, what I did in one of my solutions, was set ManualRefresh=true so that the little recycle icon would be rendered by SharePoint. Then, in my custom script that responded to the item being added (which happened in a modal dialog) I used a bit of jQuery to 'click' the icon on behalf of the user who just added the item.

// ribbon button called this to pop open dialog for adding a 'link' item:
function addlinks_display(listid, itemid) {
    var options = SP.UI.$create_DialogOptions();
    options.width = 650;
    options.height = 450;
    options.url = "/_layouts/AddLink.aspx?DispForm=1&ListID=" + listid + "&ItemID=" + itemid;
    options.dialogReturnValueCallback = Function.createDelegate(null, addlink_modalDialogClosedCallback);
    SP.UI.ModalDialog.showModalDialog(options);
}

// dialog closed callback
function addlink_modalDialogClosedCallback(result, value) {
    if (value == 1) {
        currentStatusId[currentStatusId.length] = SP.UI.Status.addStatus("Link Added",
                    "Your link was successfully added.",
                    true);
        SP.UI.Status.setStatusPriColor(currentStatusId[currentStatusId.length - 1], "Green");
        successfullyClosedMessage = value;
        refreshWebpartDetails();
    }

    if (value == '0') {
        //error logic here
    }
    setTimeout(RemoveStatus, 8000);
}

// refresh
function refreshWebpartDetails() {
    $('#WebPartWPQ1').find('#ManualRefresh').click(); //refresh webpart using SP ajax
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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