Monthly Archives: June 2014

setting incoming email settings of discussion forum list in sharepoint programmatically

hi,

 i was told to set the incoming email settings of  discussion forum list in sharepoint programmatically,so i was able to get this done with the below code:

It will take the  subsite’s name and add it with the email settings [alias] of our discussion forum list’s incoming email settings section:

private void GetListofDiscForumLists(SPWebCollection selNodewebCollec)
      {
          string restrictedFolderName = “Restricted”;
          string non_restrictedFolderName = “Non-Restricted”;

          try
          {
              foreach (SPWeb singleWeb in selNodewebCollec)
              {
                  if (singleWeb != null)
                  {
                      singleWeb.AllowUnsafeUpdates = true;

                      string singleweburl = singleWeb.Url;

                      using (SPSite objchildsite = new SPSite(singleWeb.Url))
                      {
                          using (SPWeb objchildweb = objchildsite.OpenWeb())
                          {
                              objchildweb.AllowUnsafeUpdates = true;
                              string objchildsweburl = objchildweb.Url;
                              if (objchildweb.Lists.Count > 0)
                              {
                                  for (int iListCount = objchildweb.Lists.Count – 1; iListCount > 0; iListCount–)
                                  {
                                      SPList singleDisclist = objchildweb.Lists[iListCount];

                                      if (singleDisclist.BaseTemplate == SPListTemplateType.DiscussionBoard)
                                      {
                                          SPList tempDiscForumList = singleDisclist;

                                          if (singleDisclist.CanReceiveEmail)
                                          {
                                              singleDisclist.EnableAssignToEmail = true;
                                              singleDisclist.EmailAlias = Regex.Replace(“Exp_”+objchildweb.Title, @”[^0-9a-z]”, “”, RegexOptions.IgnoreCase);
                                              SPFolder rootFolder = singleDisclist.RootFolder;
                                              rootFolder.Properties[“vti_emailusesecurity”] = 0;
                                              rootFolder.Properties[“vti_emailsaveattachments”] = 1;
                                              rootFolder.Properties[“vti_emailattachmentfolders”] = “subject”;
                                              rootFolder.Properties[“vti_emailoverwrite”] = 0;
                                              rootFolder.Properties[“vti_emailsavemeetings”] = 0;
                                              rootFolder.Properties[“vti_emailsaveoriginal”] = 1;
                                              rootFolder.Update();
                                              singleDisclist.Update();
                                          }
                                          else
                                          {
                                              throw new SPException(string.Format(“‘{0}’ can not receive mail”, singleDisclist));
                                          }
                                      }
                                  }
                              }
                              objchildweb.AllowUnsafeUpdates = false;
                          }
                      }
                      if (singleWeb.Webs.Count > 0)
                      {
                          GetListofDiscForumLists(singleWeb.Webs);
                      }
                  }

                  singleWeb.AllowUnsafeUpdates = false;
              }
          }
          catch (Exception expppo)
          {
              throw;
          }
     

hope this helps someone!

happy SharePointing!

Advertisements

Iterate through sub sites in a site collection

recently i was told to iterate through all the subsites and all the doc libs in a  site collection.

so, i had written the below code  in a  recursive way:

………………

getlistofwebsfromsitecollec(curresitecollecurl)

{

  try
     {
        SPSecurity.RunWithElevatedPrivileges(delegate()
          {
         using (SPSite _CurrentSelectedSite = new SPSite(strcurrentSelectedURLSiteNameList))
              {
                  using (SPWeb _CurrentSelectedWeb = _CurrentSelectedSite.OpenWeb())
                  {
                      _CurrentSelectedWeb.AllowUnsafeUpdates = true;
                      string mmcurrSelURL = string.Empty;
                
                  if (_CurrentSelectedWeb.Webs.Count > 0)
                   {
                      SPWebCollection childWebsofSiteCollec = _CurrentSelectedWeb.Webs;

                     GetListofWebs(childWebsofSiteCollec);

  }

 

 private void GetListofWebs(SPWebCollection childWebsofSiteCollect)
    {
        foreach (SPWeb singleSelectedWeb in childWebsofSiteCollect)
        {
            _mCurrSubWebTitleRetrieve = singleSelectedWeb.Title.ToString(); // (paramcurrSelectedNodeNameforSubSite)))
            _mCurrSubWebTitleURLRetrieve = singleSelectedWeb.Url.ToString();
            if (_mCurrSubWebTitleRetrieve != “Search”)
            {                                             

                try
                {
                    dttableGrpNamenAccessRights = RetrieveGroupNamesnApplyAccessRights(_mCurrSubWebTitleRetrieve, _mCurrSubWebTitleURLRetrieve);
                }
                catch (Exception expinRetrievenApplyPerms)
                {
                    LogthisError(expinRetrievenApplyPerms);
                }
                //NOW APPLY THE PERMISIONS
                try
                {
                   
                    dataTableGrpNamenAccessRightsContentType = RetrieveGroupNamesnApplyAccessRights(_mCurrSubWebTitleRetrieve, _mCurrSubWebTitleURLRetrieve);
                }
                catch (Exception expinRetrievenApplyPermsforCType)
                {
                    LogthisError(expinRetrievenApplyPermsforCType);
                }
            }
            try
            {
                GetListofWebs(singleSelectedWeb.Webs);
            }
            catch (Exception expinGetlistofwebs)
            {
                LogthisError(expinGetlistofwebs);
            }
        }
    }

}

}

 

Event receiver in SP 2013

hi,

below code explains how to apply permissions on an  item in a document library when an upload happens.using  a itemupdated event receiver in SP 2013 using VS 2012 :

1) it has removed all the role associations for the document . this includes entire group and  entire users.

2)it has also applied permissions on the  entire “everyone” users on the AD with  read permissions.

hope this helps someone.

using System;
using System.Web;
using System.Web.Configuration;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.Administration;

namespace testt
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class tests1: SPItemEventReceiver
    {

        #region  variables

        #endregion  variables

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            SPUser currUser = properties.Web.CurrentUser;
            SPFieldUserValue flduserValue = new SPFieldUserValue(properties.Web, currUser.ID, currUser.Name);

            try
            {
                string currentuserIdd = properties.CurrentUserId.ToString();

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    SPSite CurrentSite = new SPSite(properties.SiteId);
                    SPWeb CurrentWeb = CurrentSite.OpenWeb(properties.RelativeWebUrl);
                    SPList CurrentList = CurrentWeb.Lists[properties.ListId];
                    SPListItem CurrentListItem;

                    SPWeb myRootWeb = CurrentSite.RootWeb;

                    CurrentListItem = CurrentWeb.Lists[properties.ListId].GetItemById(properties.ListItem.ID); 

                    string _mCurrentUserID = string.Empty;

                   
                   
                 //   SPGroup EnPUsersGroupinBIF = CurrentWeb.SiteGroups[“EnPUsers”];

                    if (CurrentListItem != null)
                    {
                        {
                            try
                            {

                                {
                                    //classification column a single line of text data type
                                    selectedClassificationValue = CurrentListItem[CLASSIFICATION].ToString();
                                }
                            }
                            catch (Exception explnc)
                            {
                                throw;
                            }
                            if (!string.IsNullOrEmpty(selectedClassificationValue) && selectedClassificationValue == “Confidential” && selectedClassificationValue != “–Select–“)
                            {
                                CurrentWeb.AllowUnsafeUpdates = true;

                                CurrentListItem.BreakRoleInheritance(false);

                                //Break the ITEM Inheritence and Remove the other default groups 2) get the taxonomy from the document library 3)                        //Break the Inheritence

 

              //SPUser mallusers = properties.Web.EnsureUser(“NT AUTHORITY\\authenticated users”); //all the enp users would be getting ReadOnly access to this document

                                                         

                                SPRoleAssignmentCollection SPRoleAssColn = CurrentListItem.RoleAssignments;
                                for (int i = SPRoleAssColn.Count – 1; i >= 0; i–)
                                {
                                    SPRoleAssignment roleAssignmentSingle = SPRoleAssColn[i];
                                    System.Type t = roleAssignmentSingle.Member.GetType();
                                    if (t.Name == “SPGroup” || t.Name == “SPUser”)
                                        SPRoleAssColn.Remove(i);
                                }                                                       

                                                        SPRoleAssignment mroleAssignmentforCurrUser = new SPRoleAssignment(currUser);
                                                        SPRoleDefinition mroleDefinitionforCurrUser = CurrentWeb.RoleDefinitions[“Contribute”];
                                                        mroleAssignmentforCurrUser.RoleDefinitionBindings.Add(mroleDefinitionforCurrUser);
                                                        CurrentListItem.RoleAssignments.Add(mroleAssignmentforCurrUser);

                                                        this.EventFiringEnabled = false;

                                                        //below code is working on 4th-june-2014 for modified by 4-june 9:30 pm
                                                        CurrentListItem[“Author”] = currentuserIdd;
                                                        CurrentListItem.Update();

                                                        CurrentListItem[“Editor”] = currentuserIdd;
                                                                                                                CurrentListItem.Update();
                                                        

                                                        this.EventFiringEnabled = true;
                                                        CurrentWeb.AllowUnsafeUpdates = false;
                            }
                                                        else if (!string.IsNullOrEmpty(selectedClassificationValue) && selectedClassificationValue != “Confidential” && selectedClassificationValue != “–Select–” && selectedClassificationValue == “Public”)
                            {
                                try
                                {
                                    CurrentWeb.AllowUnsafeUpdates = true;
                                    CurrentListItem.BreakRoleInheritance(false)                               
                                                     ///

                                    SPRoleAssignmentCollection SPRoleAssColnPub = CurrentListItem.RoleAssignments;
                                    for (int i = SPRoleAssColnPub.Count – 1; i >= 0; i–)
                                    {
                                        SPRoleAssignment roleAssignmentSingle = SPRoleAssColnPub[i];
                                        System.Type t = roleAssignmentSingle.Member.GetType();
                                        if (t.Name == “SPGroup” || t.Name == “SPUser”)
                                            SPRoleAssColnPub.Remove(i);
                                    }

                                    SPUser allusers = CurrentWeb.EnsureUser(“NT AUTHORITY\\authenticated users”);
                                    SPRoleAssignment AllBIFRoleassignment = new SPRoleAssignment(allusers);
                                    SPRoleDefinition biffroledefi = CurrentWeb.RoleDefinitions.GetByType(SPRoleType.Reader);
                                    AllBIFRoleassignment.RoleDefinitionBindings.Add(biffroledefi);
                                    CurrentListItem.RoleAssignments.Add(AllBIFRoleassignment);

 

                                    SPRoleAssignment mroleAssignmentforCurrUser = new SPRoleAssignment(currUser);
                                    SPRoleDefinition mroleDefinitionforCurrUser = CurrentWeb.RoleDefinitions[“Contribute”];
                                    mroleAssignmentforCurrUser.RoleDefinitionBindings.Add(mroleDefinitionforCurrUser);
                                    CurrentListItem.RoleAssignments.Add(mroleAssignmentforCurrUser);

                                    this.EventFiringEnabled = false;

                                    CurrentListItem[“Author”] = currentuserIdd; 

                                    CurrentListItem.Update();
                                   
                                    CurrentListItem[“Editor”] = currentuserIdd; 
                                    CurrentListItem.Update();
                                    this.EventFiringEnabled = true;
                                    CurrentWeb.AllowUnsafeUpdates = false;
                                }
                                catch (Exception expforPublicdocGroup) { LogthisError(expforPublicdocGroup); }
                            }
                                      }
                    }
                });
            }
            catch (Exception expforAccessConfid)
            {
                LogthisError(expforAccessConfid);
            }
        }

              public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
        }

        public void LogthisError(Exception Expfor)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                WebPartsLoggingService.LogError(“Malat”, Expfor.StackTrace);

            });
        }

        public object _mAccessRights { get; set; }
    }

 

}