Thursday, 13 March 2014

insert lookup field in sharepoint custom list using c#

 SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists[ListName];
                        SPListItem newitem = list.Items.Add();
                        newitem["Title"] = "New lookup";
                        SPFieldLookupValue newValue = new SPFieldLookupValue(1, "Title1");
                        newitem["Title1"] = newValue;
                        newitem.Update();
                    }
                }
            });
Continue Reading...

get all lookup value from sharepoint customlist using c#

 List<string> myCollection = new List<string>();
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists[ListName];
                        int i = 0;
                        foreach (SPListItem item in list.Items)
                        {
                            SPListItem spitem = list.Items[i];
                            SPFieldLookupValue group = new SPFieldLookupValue(spitem[LookupColumnName].ToString());
                            string lookedUpItemTitle = group.LookupValue;
                            myCollection.Add(lookedUpItemTitle);
                            i++;
                        }
                    }
                }
            });
Continue Reading...

Saturday, 1 March 2014

Check the current Login user in sharepoint Group using c#

 
SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                  
                    SPWeb theSite = SPControl.GetContextWeb(Context);
                    SPUser theUser = theSite.CurrentUser;
                    string CurrentUserLoginName = theUser.LoginName;
                    string siteUrl = SPContext.Current.Web.Url;
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPGroupCollection groupCollection = web.SiteGroups;
                            foreach (SPGroup group in groupCollection)
                            {
                                SPUserCollection userCollection = group.Users;
                                string GroupName = group.LoginName;
                                if (GroupName == GrpName)
                                {
                                    foreach (SPUser user in userCollection)
                                    {
                                        string UserName = user.LoginName;
                                        if (UserName == CurrentUserLoginName)
                                        {
                                          
                                        }
                                    }
                                }
                            }

                        }
                    }
                });
Continue Reading...

Convert multiline text to string in sharepoint using c#

The below code is convert the SharePoint customList multiLine text column to string using c#

       String description = Convert.ToString(item["Description"]);

       SPHttpUtility.ConvertSimpleHtmlToText(description, -1);
Continue Reading...

Checkin-Publish folder in SharePoint using powershell


Add-PSSnapin "Microsoft.SharePoint.PowerShell"

### GLOBAL VARS, CHANGE HERE
$listname = "Style Library"
$url = "http://Abcd:1111/sites/SampleSite/"

function approveContent ($w, $listName) {
  $list = $w.Lists |? {$_.Title -eq $listName}
  foreach ($item in $list.Items)
  {
    if(($item -ne $null) -and ($item.LockId -ne $null)) {
      $item.ReleaseLock($item.LockId)
    }
    if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }
    else { $itemFile = $list.GetItemById($item.ID) }
   
    if( $itemFile.CheckOutStatus -ne "None" ) {
      $itemFile.CheckIn("Automatic CheckIn. (Administrator)")
      if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }
      else { $itemFile = $list.GetItemById($item.ID) }
    }
    if( $list.EnableVersioning -and $list.EnableMinorVersions) {
      $itemFile.Publish("Automatic Publish. (Administrator)")
      if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }
      else { $itemFile = $list.GetItemById($item.ID) }
    }
    if( $list.EnableModeration ) {
      $itemFile.Approve("Automatic Approve. (Administrator)")
    }
  }
}

$site = Get-SPSite $url
foreach ( $web in $site.AllWebs )
{
 approveContent $web $listname
}
Write-Output "OK"
Continue Reading...

Followers

Follow The Author