1

I'm working with sharepoint 2010. I have a list view webpart with a field which is a list of user account ID. the type of the field is single line of text. What I want to do is to filter my list by the current user account ID, show only items whose field contains the current user ID. I know there is the function [Me] but it doesn't work in that case because the field type is single line of text and not people or group. I don't find any function in sharepoint which returns the current user account ID.

Thanks for your help!

1

from my understanding there is no ootb method for what you want to achive! the coded method could utalize JavaScript to show hide dependant on the column values like the ids.

The simpler solution would be to create a webpart, within the visual webpart add a SPGridView. With that you can then do a checkup on the items.

iv already shown how to create a gridview within a visual webpart here:

How to display list on application (aspx) page by visual studio?

you would also need to add this code to calll the gridview binding event to check up against each item:

dont know if it would work but it needs fine tuning! i could be wrong and it would work right away as i havent tested it.... the cell id is the column index number of the field that holds the ID's , were seeing if the current user is in the text field of cell 4 (column number 5 as we start from cell 0 - 4). if the id is within the cell than show the row else hide.

.aspx

<SharePoint:SPGridView ID=”oGrid” runat=”server” AutoGenerateColumns=”true” >
</SharePoint:SPGridView>

.cs

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace VisualWebPartProject9.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/VisualWebPartProject9/VisualWebPart1/VisualWebPart1UserControl.ascx";

        private SPUser currentUser;

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);

            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb("yourSubSiteName"))
                {
                    SPList list = web.Lists["list name"];
                    SPListItemCollection items = list.Items;

                    SPGridView oGrid = new SPGridView();
                    oGrid.DataSource = items.GetDataTable();
                    oGrid.RowDataBound += new GridViewRowEventHandler(oGrid_RowDataBound);
                    oGrid.DataBind();

                    currentUser = new SPUser();
                    currentUser = web.CurrentUser;
                }
            }
        }

        private void oGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells[4].Text.Contains(currentUser.ID.ToString()))
            {
                e.Row.Visible = true;
            }
            else
            {
                e.Row.Visible = false;
            }
        }
    }
}

as we are using SPGridView it will make the webpart look exactly like the sharepoint document library.

1

I think I got something like this working --

  • a library, with a simple text field (it's pulled in as a secondary column with a lookup field, but I think it still behaves like a text field), with user names (eg, John Smith)
  • list view web part on a page for that library
  • a "current user" filter, set to send filter values to the list view web part, on the text field
  • set the properties of the "current user" filter to use "SharePoint profile value for current user" rather than "Current user name"
    • in our case, the profile value is "Name" - our users are synchronized from Active Directory, and that field seems to hold a concatenated value for the user name (no idea at this point if that's a valid assumption for all users, but it's a start)
    • it may be more elegant to make the text field contain the userid (which is unique), and use that value for a filter

Hope this helps.

0

I have come across similar problems to the one you're facing now. And in pretty much all cases I did the following:

  1. I created an additional person/group field
  2. made a view containing the entire list with only the two different type of user fields
  3. used the "quick edit" functionality on this view
  4. Copied the contents from this list to excel
  5. Used an Excel formula to convert from one to the other
  6. Copied back

Simple, but effective.

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.