Open modal dialog and refresh the parent page from custom form SharePoint 2010

What

You need to open a SharePoint 2010 popup (SPModalDialog) and refresh the parent page on close.

So What

End Users get griefed when they add an amazing new bit of content to SharePoint, but after submitting it’s not immediately visible on the page they just “came from” (even though geeks know the score).

Now What

Add the following code (assuming you are up to snuff on the basics of the SP Modal Dialog methods – here’s a good primer):

<script type="text/javascript">
    function openMyDialog(itemid,title) {
        var options = SP.UI.$create_DialogOptions();
        var layoutsUrlView = SP.Utilities.Utility.getLayoutsPageUrl('WebParts/MyWebPart/ViewDialog.aspx')
        options.url = layoutsUrlView + "?itemid=" + itemid + "&title=" + title;
        options.autosize = true;
        options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
        SP.UI.ModalDialog.showModalDialog(options);
    }
    function CloseCallback(result, target) {
        location.reload(true);
    }
</script>

Now when you close the popup, the parent page is refreshed.

Incoming search terms:

SharePoint 2010 101 Code Samples

The SharePoint 2010 101 Code Samples set of examples is an excellent starting point for Developing with SharePoint.

Each code sample is part of the SharePoint 2010 101 code samples project. These samples are provided so that you can incorporate them directly in your code.

Each code sample consists of a standalone project created in Microsoft Visual Studio 2010 and demonstrates a distinct feature or feature set. Each sample includes comments describing the sample and the expected results. Each sample also contains comments that explain how to set up your environment so that the sample code runs, where necessary.

Microsoft SharePoint 2010 gives you the tools needed to create powerful applications. These managed code (C#, VB.NET, JavaScript, XML) samples can assist you in creating your own applications that perform specific functions or as a starting point to create more complex solutions.

To open a solution:
1. Start Microsoft Visual Studio 2010.
2. On the File menu, click Open, and then click Project/Solution.
3. Navigate to the folder containing the .sln file, select it, and then click Open.

To run a solution:
1. In the solution files, read and follow the comments that describe how to set up your environment if necessary.
2. On the Build menu, click Build Solution.
3. When you have a successful build, right-click the project in the Solution Explorer window, and then click Deploy.

List of all Examples in the pack:

SharePoint 2010 Developing Styled Master Pages
This sample creates and deploys branded master pages to customize SharePoint sites, including custom stylesheets and images.

SharePoint 2010 Developing JQuery-Enabled Web Parts
This sample develops a Web Part that uses the JQuery library to display items from a SharePoint list.

SharePoint 2010 Hosting Silverlight Applications that Call Azure Services
This sample calls WCF web services that are hosted in Windows Azure from Silverlight applications that are stored in SharePoint.

SharePoint 2010 Developing AJAX-Enabled Web Parts
This sample creates a SharePoint Web Part that uses Ajax UpdatePanel and UpdateProgress controls to call server-side methods.

SharePoint 2010 Leveraging HTML5 Objects in SharePoint
This sample uses custom SharePoint master pages to enable IE9 and other compliant browsers to render HTML5 tags such as <audio>.

SharePoint 2010 Creating List Items from Silverlight
This sample creates SharePoint list items by calling the SharePoint Client Object Model from Silverlight applications.

SharePoint 2010 Developing Web Templates
This sample creates web templates, which are similar to SharePoint site definitions, but can be used in sandboxed solutions.

SharePoint 2010 Creating Document Sets Programmatically
This sample creates new SharePoint document sets and configures their properties.

SharePoint 2010 Developing List Definitions
This sample uses declarative programming to create a SharePoint list definition and an instance of that list.

SharePoint 2010 Developing Custom Navigation Providers
This sample creates and deploys two links to the top link bar on a SharePoint site.

SharePoint Online Accessing Web Services
This sample connects to SharePoint Online, authenticates by using claims authentication, and then displays the contents of a SharePoint Online list.

SharePoint 2010 Querying SQL Azure Data from Web Parts
This sample queries a SQL Azure database from code in a SharePoint Web Part.

SharePoint 2010 Developing Sequential Workflows
This sample develops SharePoint sequential workflows based on items in task lists.

SharePoint 2010 Creating Custom Field Types
This sample creates a new field type that adds options for users who create new columns in SharePoint lists, libraries, or content types.

SharePoint 2010 Creating Items in Lists from External WCF Services
This sample creates SharePoint list items by calling a method in a WCF service that uses the SharePoint List Data Retrieval Web Service.

SharePoint 2010 Developing Workflow Activities
This sample creates and deploys custom workflow activities that can be used in SharePoint Designer to extend workflows.

SharePoint 2010 Performing Cross-List Queries
This sample uses the SPSiteDataQuery class to find items from all the lists in a SharePoint site.

SharePoint 2010 Creating Custom Timer Jobs
This sample creates and schedules a SharePoint timer job that runs code at regular intervals.

SharePoint 2010 Developing Starter Master Pages
This sample sets master pages for SharePoint sites by using feature receivers, and deploys master pages without styles or images as starters for branded master pages.

SharePoint 2010 Updating SQL Azure Records from Web Parts
This sample saves changes to a SQL Azure record by using code in a SharePoint Web Part.

SharePoint 2010 Uploading SharePoint Library Content to Azure Storage
This sample uploads files from a SharePoint document library to Windows Azure storage.

SharePoint 2010 Displaying Video Files Stored in Azure
This sample displays videos stored in Windows Azure in Silverlight applications.

SharePoint 2010 Creating SQL Azure Records from Web Parts
This sample inserts records into SQL Azure tables by using code in SharePoint Web Parts.

SharePoint 2010 Developing Feature Receivers
This sample develops a Feature receiver that performs an action when Features activate and cleans up when Features deactivate.

SharePoint 2010 Calling Azure Services from Event Receivers
This sample calls WCF web services hosted in Windows Azure from SharePoint event receivers.

SharePoint 2010 Using JavaScript and CAML to Query Lists
This sample uses the SharePoint Client Object Model to display the details of all the items in a SharePoint list.

SharePoint 2010 Creating Custom SharePoint Service Applications
This sample returns the current weekday by using a custom service application for SharePoint.

SharePoint 2010 Creating Content Types Programmatically
This sample creates SharePoint content types nondeclaratively in code.

SharePoint 2010 Developing Connected Web Parts
This sample creates two SharePoint Web Parts that you can connect to exchange information.

SharePoint Online Authenticating Using the Client-Side Object Model
This sample connects to, and authenticates in, SharePoint Online.

SharePoint 2010 Using REST to Discover the Contents of Excel Worksheets
This sample displays the names of tables that are in a spreadsheet by calling RESTful Excel Web Services.

SharePoint 2010 Displaying User Profile Pictures Programmatically
This sample evaluates and displays the pictures of all users who have set profile pictures in a Web Part.

SharePoint 2010 Using LINQ in REST Requests
This sample uses LINQ queries to return filtered lists of items from SharePoint lists.

SharePoint 2010 Accessing SharePoint Lists from External WCF Services
This sample writes a WCF service that returns all the items in SharePoint lists, and includes a sample client console application to test the service.

SharePoint 2010 Calling Azure Services from Web Parts
This sample calls WCF web services hosted in Windows Azure from SharePoint Web Parts.

SharePoint 2010 Performing Searches from Silverlight
This sample calls the SharePoint Search web service from a Silverlight application.

SharePoint 2010 Performing Searches from Web Parts
This sample calls a SharePoint Search or FAST Search service application from a Web Part.

SharePoint 2010 Creating Content Organizer Rules Programmatically
This sample creates and configures content organizer rules for content types in SharePoint document libraries.

SharePoint 2010 Creating Taxonomies Programmatically
This sample adds SharePoint groups, term sets, and terms to a term store programmatically.

SharePoint 2010 Developing Custom Field Controls
This sample creates custom field controls that display and edit fields on SharePoint publishing sites.

SharePoint 2010 Developing Application Pages
This sample creates and deploys a simple application page that displays information about the current SharePoint site, and modifies its description.

SharePoint 2010 Programmatically Reading User Profile Properties
This sample obtains properties from all SharePoint user profiles in your organization.

SharePoint 2010 Developing Event Receivers
This sample develops and registers an event receiver that intercepts SharePoint list item events such as ItemAdded and ItemUpdating.

SharePoint 2010 Managing Document Sets Programmatically
This sample reads properties from all the document sets in a given SharePoint document library.

SharePoint 2010 Deleting SQL Azure Records from Web Parts
This sample deletes records in SQL Azure by using code in SharePoint Web Parts.

SharePoint 2010 Calling Azure Services from Custom Workflow Activities
This sample calls WCF web services hosted in Windows Azure from code in SharePoint workflows.

SharePoint 2010 Accessing List Items from Silverlight
This sample returns items in SharePoint lists (in Silverlight applications) by using the SharePoint Client Object Model.

SharePoint Online Creating Documents Using Word, PowerPoint, or OneNote Web App
This sample creates custom ribbon actions that use JavaScript and Office Web Apps to create new Word, PowerPoint, and OneNote documents.

SharePoint Online Creating and Deploying Sandboxed Workflow Activities
This sample creates a workflow activity that functions in a sandboxed solution on SharePoint Online.

SharePoint 2010 Importing Content by Using the Content Deployment API
This sample imports content from CMP files into SharePoint lists by calling the Content Migration API.

SharePoint 2010 Developing Connected Silverlight Web Parts
This sample creates interconnected custom SharePoint Web Parts that can host Silverlight applications that exchange information.

SharePoint 2010 Programmatically Finding Tagged Items
This sample locates terms that match input strings and then locates all items tagged with those terms in SharePoint lists.

SharePoint 2010 Logging Site Events Programmatically
This sample develops and registers event receivers to intercept web events such as SiteDeleted and WebMoved, and logs those events to a list for auditors.

SharePoint 2010 Using REST to Obtain Excel Charts
This sample obtains image files of charts from Excel spreadsheets via REST.

SharePoint 2010 Calling WCF Services from Web Parts
This sample calls a WCF service that retrieves data after users click a button in a SharePoint Web Part.

SharePoint 2010 Deleting Items in Lists from External WCF Services
This sample creates a WCF service that finds a SharePoint item and deletes it.

SharePoint 2010 Using JavaScript to Edit and Save Values in Items
This sample uses the SharePoint Client Object Model to change items in SharePoint lists.

SharePoint 2010 Developing Page Layouts
This sample creates and deploys custom page layouts for content types in SharePoint publishing sites.

SharePoint 2010 Declaring Records Programmatically
This sample can determine whether a document is a record and declare it as a record.

SharePoint 2010 Calling Azure Services from Timer Jobs
This sample calls WCF web services hosted in Windows Azure from SharePoint timer jobs.

SharePoint 2010 Developing Solution Validators
This sample develops SharePoint solution validators that check activating user solutions and help verify sandboxed solutions.

SharePoint 2010 Developing Custom Expiration Actions
This sample specifies custom actions to take and code to run after a document expires against a SharePoint information management policy.

SharePoint 2010 Retrieving List Contents and Parsing Atom Responses
This sample gets all the items in a SharePoint list by using the RESTful List Data web service.

SharePoint Online Creating and Deploying Sandboxed Event Receivers
This sample responds to item events (such as ItemAdded) in sandboxed event receivers.

SharePoint 2010 Using REST to Query Data Ranges in Excel Worksheets
This sample gets and displays data from a date range in an Excel spreadsheet by querying RESTful Excel Web Services.

SharePoint 2010 Developing Ribbon Actions
This sample adds a button control (to the SharePoint ribbon) that runs JavaScript when clicked.

SharePoint 2010 Developing Delegate Controls
This sample creates an ASP.NET user control to replace the standard SharePoint Global Navigation.

SharePoint 2010 Working with Disposable Objects
This sample disposes SPWeb and SPSite objects properly so that they do not unnecessarily consume memory.

SharePoint 2010 Logging Data to the Developer Dashboard
This sample uses monitored scopes to log information to developer dashboards, and includes scripts to enable and disable those dashboards.

SharePoint Online Creating Excel Worksheets by Using Excel Web App
This sample creates custom ribbon actions that use JavaScript and Excel Web Apps to create new spreadsheets.

SharePoint 2010 Using JQuery to Retrieve List Contents in JSON
This sample uses the JQuery library to obtain and display items in SharePoint lists.

SharePoint 2010 Retrieving Single List Items in REST Requests
This sample gets a single item from a SharePoint list by using the RESTful List Data web service.

SharePoint 2010 Calling WCF Services from Custom Workflow Activities
This sample calls a WCF service from a SharePoint workflow that starts after a new item is created, and then modifies the item body field of that workflow.

SharePoint 2010 Retrieving List Contents in JSON Format and Parsing Responses
This sample gets items from SharePoint lists in JSON format, parses the responses, and then displays the item properties.

SharePoint 2010 Using JavaScript to Show Dialog Boxes
This sample uses the SharePoint Client Object Model to display an application page as a dialog box.

SharePoint 2010 Calling WCF Services from Event Receivers
This sample calls a WCF service from an event receiver; after a new item is added to a SharePoint list, the service returns data that is appended to the body of that new item.

SharePoint 2010 Performing Cached Cross-Site Queries
This sample uses the SharePoint PortalSiteMapProvider class to perform high-performance cross-site queries.

SharePoint 2010 Using REST to Create a SharePoint and Bing Maps Mashup
This sample uses JavaScript to integrate data from Excel spreadsheets in SharePoint with Bing Maps.

SharePoint 2010 Using JavaScript to Enable Notifications
This sample uses SharePoint status and notifications to feed back information to users.

SharePoint 2010 Using JavaScript to Create Lists
This sample uses the SharePoint Client Object Model to create SharePoint lists with JavaScript.

SharePoint 2010 Using JavaScript to Retrieve and Interrogate Items in Lists
This sample uses the SharePoint Client Object Model with a CAML query to return matching items from lists.

SharePoint 2010 Developing Branded Media Controls
This sample brands a Media Field Control on a SharePoint site.

SharePoint 2010 Using JavaScript to Delete Items from Lists
This sample uses the SharePoint Client Object Model to delete items from a SharePoint list with JavaScript.

SharePoint 2010 Using JavaScript to Update Site Properties
This sample uses the SharePoint Client Object Model to set titles and descriptions for SharePoint sites.

SharePoint 2010 Using JavaScript to Get Details About Sites
This sample uses the SharePoint Client Object Model to display information about the current SharePoint site.

SharePoint 2010 Developing Editor Web Parts
This sample modifies a Web Part properties sheet to include an Editor Web Part that enables users to choose from all the lists in the SharePoint site.

SharePoint 2010 Calling RESTful SharePoint Services From Desktop Applications
This sample uses a service reference to connect to the List Data Retrieval Web Service and display lists of SharePoint items in a Data Grid.

SharePoint Online Accessing Current User Information in Sandboxed Solutions
This sample creates a Web Part that functions in a sandboxed solution on SharePoint Online and gets information about the current user.

SharePoint 2010 Canceling Synchronous Events
This sample checks the properties of synchronous events and cancels them to prevent users from deleting SharePoint items.

SharePoint 2010 Calling WCF Services Hosted in SharePoint
This sample calls a WCF service that is hosted by Sharepoint, and whose code uses the SharePoint Foundation Server-Side Object Model.

SharePoint 2010 Developing Ribbon Drop Down Controls
This sample uses a drop-down control on the SharePoint ribbon to forward users to the selected list.

SharePoint 2010 Developing State Machine Workflows
This sample develops state machine workflows based on documents in SharePoint document libraries and items in tasks lists.

SharePoint Online Deploying Sandboxed Content Types and List Definitions
This sample creates a Web Part that functions in a sandboxed solution on SharePoint Online and uses code to create content types and list definitions.

SharePoint 2010 Developing Sandboxed Web Parts
This sample creates a Web Part in a sandboxed solution, and includes Panels to hide controls, radio buttons, and the Render method.

SharePoint 2010 Using JavaScript to Get Details About Site Collections
This sample uses the SharePoint Client Object Model to display information about the current site collection.

SharePoint 2010 Calling WCF Services from Timer Jobs
This sample calls a WCF service from a custom timer job that creates a new announcement when it runs.

SharePoint 2010 Developing Site Definitions
This sample creates a custom SharePoint site definition that specifies Content Editor and Image Viewer Web Parts on a Web Part page.

SharePoint 2010 Developing Custom Expiration Formulae
This sample calculates an expiration date for a SharePoint information management policy by using a custom expiration formula.

SharePoint Online Creating and Deploying Sandboxed Web Parts
This sample creates a Web Part that checks whether it is in a sandboxed solution and displays the title of the SharePoint site.

SharePoint 2010 Exporting Content by Using the Content Deployment API
This sample exports content from SharePoint lists by calling the Content Migration API.

SharePoint Online Creating and Deploying Sandboxed Feature Receivers
This sample creates a feature receiver that works in a SharePoint Online sandboxed solution.

Incoming search terms:

SharePoint 2010 Get Current Username / Logged in User

Have been exploring the SharePoint Client Object Model and in particular the Javascript Object Model

While there are methods to get the current user info, you can also simply borrow the logged in username from the top right of the screen:


<script type="text/javascript">// <![CDATA[
 var Username = document.getElementById("zz16_Menu").innerHTML ;
 var end = Username.indexOf("<");
 var nameOnly = Username.substring(8, end);
 document.write(nameOnly);

// ]]></script>

This technique is a little complex for my liking however, so here’s a snappier way to grab the current login name (using Jquery):


var Username = $("#zz16_Menu").text();
document.write(Username);

Note that the #zz16 identifier will vary based on your Master page. Use the Firefox Web Developer tools or Internet Explorer F12 tools to find out what the ID should be on your SharePoint site.

Using this type of technique is definitely more brittle than actually using a CAML query however in my mind would be more performant than actually doing a lookup in the DB. Would love to know if someone knows the scoop on that..

Update 05/12/2011

As my colleague Colin pointed out – the name that appears on the top right can really be anything and can change – for example if a user gets married and their name changes. A better approach would be to retireve the value programmatically using SPServices ( http://spservices.codeplex.com/wikipage?title=$%28%29.SPServices.SPGetCurrentUser ). As of version v0.6.1, you can also request the ID of the user by specifying fieldName: “ID”.  Much more stable to use in most scenarios:

$().SPServices.SPGetCurrentUser({
	fieldName: "ID",
	debug: false
});

Incoming search terms:

Recent Comments

  • ChristopheD

    Hi Keith
    Sorry for the late answer but I had some urgent problems to solve.
    It’s ok now the feature has been removed and there is no more message.
    thanks a lot for your help and time.

  • Keith Tuomi

    Don’t worry, I often am unable to wrap my head around SharePoint too. ;)

    It really does sound like what you need a is Workflow. A workflow let’s you perform business logic like you described – check out the getting started guide at http://msdn.microsoft.com/en-us/library/office/jj163917(v=office.15).aspx. SharePoint includes Workflow out of the box, all you need to do to get started is download a free copy of SharePoint Designer.

Follow me on Twitter