SharePoint, Get User Account SPUser, SPFieldUserValue From List

Try this.

 

        public static string GetUserAccountFromList(SPListItem item)

        {

            SPFieldUserValue fieldValue = null;

            SPUser user = null;

 

            SPFieldUser UserColumn = (SPFieldUser)item.Fields.GetField("YourUserFieldName");

            fieldValue = UserColumn.GetFieldValue(item[fieldName].ToString()) as SPFieldUserValue;

 

            if (fieldValue != null)

                user = fieldValue.User;

 

            return user;

        }

Enjoy DDIBA~! Cool

SharePoint, Set Logs in 14 hive logs folder by programmatically

Try this code...

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint.Administration;

 

namespace YourProject

{

    public class LoggingService : SPDiagnosticsServiceBase

    {

        public static string DiagnosticAreaName = "Your Diag Area Name";

 

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()

        {

            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>

            { 

            new SPDiagnosticsArea(DiagnosticAreaName, new List<SPDiagnosticsCategory> { new SPDiagnosticsCategory(DiagnosticAreaName, TraceSeverity.Unexpected, EventSeverity.Error) })

            };

            return areas;

        }

 

        public static void Log(string source, TraceSeverity traceSeverity, EventSeverity eventSeverity, string logMessage)

        {

            SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(source, traceSeverity, eventSeverity), traceSeverity, logMessage, null);

        }

    }

}

And use this in your code like this...
        private void YourMethodOrFunction()
        {
            try
            {
Do... Run...
            }
            catch (Exception ex)
            {
                LoggingService.Log("Your Application Name", Microsoft.SharePoint.Administration.TraceSeverity.High, Microsoft.SharePoint.Administration.EventSeverity.Error, ex.Message);
            }
        }
And check your Log file in Logs folder.
Location: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS

Enjoy~! DDIBA~! Cool

 

SharePoint, Get fieldValue from SharePoint User field in List SPFieldUserValue

 

 

 

        public static SPUser GetSPUser(SPListItem item, string key)

        {

            SPFieldUser field = item.Fields[key] as SPFieldUser;

            SPFieldUserValue fieldValue =

             field.GetFieldValue(item[key].ToString()) as SPFieldUserValue;

            if (fieldValue != null)

                return fieldValue.User;

        }

Enjoy DDIBA~! Cool

SharePoint, Set or Update People Editor control value from SharePoint List

This code demostrate how to set people editor control values from  a sharepoint list

SPSite site = SPContext.Current.Site;

SPWeb myweb = site.OpenWeb();

SPList mylist = myweb.Lists["MyList"];

SPQuery query = new SPQuery();

query.Query = "<Where><Eq><FieldRef Name='ID'/>" +

              "<Value Type='Text'>" + e.Value.ToString() + "</Value></Eq></Where>";

 

SPListItemCollection items = mylist.GetItems(query);

  foreach (SPListItem item in items)

    {

 

     try

      {

 

        this.txtTitle.Text = item["Title"].ToString();

        this.txtStart.SelectedDate =Convert.ToDateTime(item["EventDate"].ToString());

       }

        catch (Exception ex)

       {

         Response.Write(ex.Message.ToString());

       }

 

 

ArrayList _arrayList = new ArrayList();

SPFieldUserValueCollection users = item["People"asSPFieldUserValueCollection;

  if (users != null)

     {

       foreach (SPFieldUserValue user in users)

         {

 

          PickerEntity _pickerEntity = new PickerEntity(); // PickerEntitiy use using Microsoft.SharePoint.WebControls

          _pickerEntity.Key = user.User.LoginName;

          _pickerEntity.IsResolved = true;

          _arrayList.Add(_pickerEntity);

          }

        pplEditor.UpdateEntities(_arrayList);

 

      }

}  

 

ENJOY DDIBA~! Cool

SharePoint, Set or Update People Editor control value in SharePoint

This code demostrate how to get people editor control values and insert a sharepoint list

SPWeb mySite = SPContext.Current.Web;

SPListItemCollection listItems = mySite.Lists["myList"].Items;

 

SPListItem item = listItems.Add();

 

item["Title"] = this.txtTitle.Text.ToString();

item["Location"] = this.lblLocations.Text.ToString();

item["EventDate"] = txtStart.SelectedDate ;

 

string[] UsersSeperated = pplEditor.CommaSeparatedAccounts.Split(',');

SPFieldUserValueCollection UserCollection = newSPFieldUserValueCollection();

foreach (string UserSeperated in UsersSeperated)

   {

    mySite.EnsureUser(UserSeperated);

    SPUser User = mySite.SiteUsers[UserSeperated];

    SPFieldUserValue UserName = new SPFieldUserValue(mySite, User.ID, User.LoginName);

    UserCollection.Add(UserName);

   }

item["people"] = UserCollection;

item.Update();

 

Enjoy DDIBA~! Cool

SharePoint, Postback Event when using PeopleEditor

I'm using the SharePoint PeopleEditor to select a user and when the user is selected I want to populate a few fields with information about that user, I retrieve the data from AD. The problem I had was how do I make the PeopleEditor send a signal to my code that a value has been set?! There is no event on the PeopleEditor control which is fired when a value is set.

I first asked myself this question about a month ago, but as I have been unable to find an answer I added a button to my form which says "Retrieve Userinformation". This is not the normal way of UI design and during a demo for the customer today they made a remark about it, so I decided to take another look at this problem.

It took me almost an hour to realize that there is a property called AutoPostBack on some server controls. When you set this property a Postback will be generated when a value is set in the control. As you may understand from my lack of knowledge in this area ASP.NET UI design is not my primary competence, but in most cases I know enough to get the job done.

<SharePoint:PeopleEditor AutoPostBack="true" ID="peUser" runat="server" />

In the case of the PeopleEditor this means that when you type a username and click Check names (or hit enter) or use the Addressbook to select a user you will receive a generic postback, no specific event i raised by the page is reloaded. Therefore you can have code in your Page_Load which checks if a value has been selected and take some action.

string accountName = null;
if (peUser.ResolvedEntities.Count > 0)
{
    PickerEntity entity = (PickerEntity)peUser.ResolvedEntities[0];

    accountName = entity.Key;
    int pos = accountName.IndexOf('\\');
    accountName = accountName.Substring(pos + 1);

    // take some action based on accountName

}

In essence the AutoPostBack acts as an event generator which lets you perform an action when the value of the control is set.

Enjoy DDIBA~! Cool

Useful Links 512

  1. Building Blocks #1: Silverlight Slideshow Viewer for SharePoint
  2. SharePoint Disposing Myth: You have to dispose SPWeb explicitly!
  3. Register SharePoint themes by using a feature
  4. SharePoint: Strike Out Canceled Events in the Calendar
  5. SharePoint: Hiding the Checked Out Icon from Anonymous Users
  6. Limitations of SharePoint Web Services
  7. SharePoint Branding WhitePaper

Useful Links 508

  1. Creating a dynamic site page banner using SharePoint and Silverlight
  2. Changing Service Accounts in SharePoint
  3. New release SharePoint Objects: features and groups
  4. Get up to speed with PerformancePoint dashboards and Web Parts
  5. Another Intro to CAML Blog
  6. MOSS 2007 – backup and restore keywords and best bets

Useful Links 504

  1. Constructing the url to the SharePoint Edit Permissions page
  2. Architecture of SharePoint for ASP.Net Developers
  3. Deleting SharePoint Groups from Site Collection
  4. Scrolling Announcement(with link) web part in SharePoint
  5. Codeless SharePoint Development
  6. SharePoint and Drupal


Donate Me!

Please Help Us to Maintain This Site!

About the youngmoony