Showing posts with label SharePoint C#. Show all posts
Showing posts with label SharePoint C#. Show all posts

Sunday, 6 July 2014

Get current logged in user


    To get current logged in user for Sharepoint use this piece of code. You can use this code with custom forms where it gets the current logged in user name.


                        SPWeb getcurweb = SPControl.GetContextWeb(Context);
                        SPUser getcurlogeduser = getcurweb.CurrentUser;
                        string currentusername = getcurlogeduser.Name;



Wednesday, 4 June 2014

Check if current user belongs to particular group


To check if the current user belongs to particular group use the following code.


SPGroup Administrator_Group = SP_Web.Groups["Administrator"];
bool is_Administrator_Group =  SP_Web.IsCurrentUserMemberOfGroup(Administrator_Group.ID);


Tuesday, 17 December 2013

Get Current User Email by sharepoint 2013 Object Model


Hi,


   Here we will write sharepoint 2013 object model code to retrieve current user email id. 
Use the following code.

string strUserName = HttpContext.Current.User.Identity.Name.ToString();

string strEmail = SPContext.Current.Web.AllUsers[strUserName].Email;

First we will retrieve the user name and then we will retrieve the email id.

Monday, 16 December 2013

Different types in Updating Sharepoint List


            While creating Event Receivers or Workflows we need to update the list or list items using SPListItem method. However there are different types in updating. Most commonly used are Update and SystemUpdate. lets see the different types of updates and its use.


Update()


  • Updates the item in the database.
  • Updates the “Modified” and “Modified by” values.
  • Creates a new version.

Systemupdate()


  • Updates the item in the database(List).
  • Does not update build in columns "Modified" and "Modified By".
  • Does not increment item version.
  • Triggers the item events.

Systemupdate(true)


  • Updates data to database(List).
  • Does not update build in columns "Modified" and "Modified By".
  • Will increment Item Version.

Systemupdate(false)


  • Updates data to database.
  • Does not update build in columns "Modified" and "Modified By".
  • Does not increment item version.

UpdateOverwriteVersion()


  • Updates data to database.
  • Updates build in columns "Modified" and "Modified By".
  • Does not increment new version.

Note :


You can also disable the triggering of events by using “this.EventFiringEnabled = false;”. Do your update and enable the events again with “this.EventFiringEnabled = true;”

Sunday, 15 September 2013

Check if list exists using TryGetList method in SharePoint 2010


Now lets see how to check if list exists using TryGetList method in SharePoint.

Normally we will be using 

SPList list = web.Lists[listName]; 

to get a list.

But, it throws a null reference error if the list is not there(i.e., in case if it is being deleted from the site using browser). So we will be looping through the SPListItemCollection object and check if the list exists or would go for exception. In SharePoint 2010, a new method "TryGetList" is implemented to get the list and check if the list exists. Lets see how the code works out.


using (SPSite site = new SPSite("http://serverName:1234/"))
{
  using (SPWeb web = site.RootWeb)
     {
        SPList list = web.Lists.TryGetList("My List");
        if (list != null)
        {
          Console.WriteLine("List exists in the site");
        }
        else        

        {
          Console.WriteLine("List does not exist in the site");
        }
          Console.ReadLine();
     }
}

Friday, 31 May 2013

Change the Web URL from Alternate Access Mappings using C#


There are many types of Zones available in the SharePoint access mappings.
  • Default
  • Intranet
  • Extranet
  • Internet
  • Custom 

Using this code you can change the alternate access mappings from Default to Internet.


            string webUrl = string.Empty;
            using (SPWeb web = workflowProperties.Site.RootWeb)
            {
                using (SPSite site = web.Site)
                {
                    foreach (Microsoft.SharePoint.Administration.SPAlternateUrl altUrl in site.WebApplication.AlternateUrls)
                    {
                        if (altUrl.UrlZone == Microsoft.SharePoint.Administration.SPUrlZone.Internet)
                            webUrl = altUrl.IncomingUrl .ToString() + site.ServerRelativeUrl.ToString();
                        //fullweburl = webUrl + site.ServerRelativeUrl.ToString();
                    }
                }
            }

Friday, 24 May 2013

Set or Get value people editor control in SharePoint with C#


Get People Editor Values


This code demostrate how to get people editor control values and insert a sharepoint list or document library.



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();



Set People Editor values from Sharepoint List or library


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);

      }
}