kamagra how much to take

Customizing ListFormWebPart URL Returns

Well, I didn’t know this trick, and today a co-worker pointed it out to me, which I wish I would have known previously when writing custom interface WebParts that interact with SharePoint lists.

Let’s say that you are developing a WebPart that interacts with a list, such as something that displays your list items in a neat way that is conducive to whatever your given software requirements are. For example, I was writing a WebPart for the last week or so that built a collection of SPView objects and did custom banding of the events in a custom Calendar type view so that we could basically color code events in a calendar so that it was more similar to Outlook. It made it easy for a user then to view those grouped events because they could browse by color.
On this WebPart, I wanted to offer my users a way to directly add list items within the header of the WebPart, assuming that they had the rights to even see this link. Therefore, I had to wrap the link to the NewForm.aspx page with a boolean check using the SPListItem.EffectiveBasePermissions property to harvest the current users group membership and policies, and then use the SPBasePermissions enumeration in order to get what actions are legal for the current user that is looking at the WebPart. The thing to keep in mind about this WebPart was I didn’t have it where the ListFormWebPart typically would be, it existed in a the Pages directory. So, when a user added a new item, they were redirected back to the default list form view, this happened with all the CRUD actions when working with the list.

I had to do this quick (5 minutes) so I just through this chuck of code in to add the new item link in the header (it obviously is not entirely best practice code but that is not the subject of this post):

  1. string r00tUrl;
  2. SPList splist;
  3. SPWeb curWeb = SPControl.GetContextSite(Context).RootWeb
  4. r00tUrl = curWeb.Url;
  5. splist = curWeb.Lists["<your list>"];
  6. if (splist != null)
  7. {
  8. bool canAddItem = (splist.EffectiveBasePermissions & SPBasePermissions.AddListItems) == SPBasePermissions.AddListItems;
  9. if (canAddItem)
  10. {
  11. str.AppendFormat("
  12. " + "<a xhref={0}/Lists/<your list>/NewForm.aspx>" + "Add New Event" + "</a>", r00tUrl);
  13. str.AppendFormat("");
  14. }
  15. }

But doing so would redirect the user to the default view of the list after the added the item. I found out you have to add a small Source query string to the end of the URL to redirect the user back to the custom page, like this ?Source=/pages/CustomPage.aspx

So, my new code looks like this:

  1. string r00tUrl;
  2. SPList splist;
  3. SPWeb curWeb = SPControl.GetContextSite(Context).RootWeb
  4. r00tUrl = curWeb.Url;
  5. splist = curWeb.Lists["<your list>"];
  6. if (splist != null)
  7. {
  8. bool canAddItem = (splist.EffectiveBasePermissions & SPBasePermissions.AddListItems) == SPBasePermissions.AddListItems;
  9. if (canAddItem)
  10. {
  11. str.AppendFormat("
  12. " + "<a xhref={0}/Lists/<your list>/NewForm.aspx?Source=/pages/Calendar.aspx>" + "Add New Event" + "</a>", r00tUrl);
  13. str.AppendFormat("");
  14. }
  15. }

Works great!

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>