Exemple #1
0
        public static SPUser EnsureUser(string userName, string siteUrl)
        {
            SPUser user = null;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(() =>
                {
                    using (var site = new SPSite(siteUrl))
                    {
                        using (SPWeb currentWeb = site.OpenWeb())
                        {
                            currentWeb.AllowUnsafeUpdates = true;
                            user = currentWeb.EnsureUser(userName);
                            currentWeb.AllowUnsafeUpdates = false;
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ULSLogging.LogError(ex);
                return(null);
            }

            return(user);
        }
Exemple #2
0
        /// <summary>
        /// Get User field from SharePoint List item
        /// </summary>
        /// <param name="item">List Item</param>
        /// <param name="fieldName">Field Name</param>
        /// <returns>SharePoint SPFieldUserValue</returns>
        public static SPFieldUserValue GetUserField(SPListItem item, string fieldName)
        {
            try
            {
                if (item[fieldName] == null)
                {
                    return(null);
                }

                var currentValue = item[fieldName].ToString();
                var spfield      =
                    item.ParentList.Fields
                    .Cast <SPField>()
                    .FirstOrDefault(c => c.InternalName.Equals(fieldName, StringComparison.OrdinalIgnoreCase) ||
                                    c.Title.Equals(fieldName, StringComparison.OrdinalIgnoreCase));
                var field       = spfield as SPFieldUser;
                var fieldValues = (SPFieldUserValue)field?.GetFieldValue(currentValue);
                return(fieldValues);
            }
            catch (Exception ex)
            {
                ULSLogging.LogError(ex);
            }

            return(null);
        }
Exemple #3
0
        /// <summary>
        /// Get Credential from SSS
        /// </summary>
        /// <param name="appId">Application Id</param>
        /// <param name="adminSiteUrl">Admin Site Url</param>
        /// <returns>Credential as Dictionary string and string</returns>
        public static Dictionary <string, string> GetCredentialsFromSSS(string appId, string adminSiteUrl)
        {
            var result = new Dictionary <string, string>();

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    var siteAdmin = new SPSite(adminSiteUrl);

                    // Get the default Secure Store Service provider.
                    var provider = SecureStoreProviderFactory.Create();
                    if (provider == null)
                    {
                        throw new InvalidOperationException("Unable to get an ISecureStoreProvider");
                    }

                    var providerContext = provider as ISecureStoreServiceContext;
                    if (providerContext == null)
                    {
                        return;
                    }

                    providerContext.Context = SPServiceContext.GetContext(siteAdmin);

                    var secureStoreProvider = new SecureStoreProvider {
                        Context = providerContext.Context
                    };

                    // Create the variables to hold the credentials.
                    using (var creds = provider.GetCredentials(appId))
                    {
                        if (creds == null)
                        {
                            return;
                        }

                        var fields = secureStoreProvider.GetTargetApplicationFields(appId);
                        if (fields.Count <= 0)
                        {
                            return;
                        }

                        for (var i = 0; i < fields.Count; i++)
                        {
                            var field               = fields[i];
                            var credential          = creds[i];
                            var decryptedCredential = GetStringFromSecureString(credential.Credential);
                            result.Add(field.Name, decryptedCredential);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ULSLogging.LogError(ex);
            }

            return(result);
        }
Exemple #4
0
        /// <summary>
        ///     Get requested culture
        /// </summary>
        /// <param name="context">Http context</param>
        /// <returns>Culture Info</returns>
        private void SetCulture(HttpContext context)
        {
            // The key of current selected language in the cookies.
            string strKeyName = "Stada_LangSwitcher_Setting";
            // Current language.
            string strLanguage = "vi-VN";
            string strLanguageFromQueryString = string.Empty;

            try
            {
                if (!string.IsNullOrEmpty(context.Request.QueryString[KeyCulture]))
                {
                    strLanguageFromQueryString = context.Request.QueryString[KeyCulture];
                }

                // Set the current language.
                if (context.Session != null && context.Session[strKeyName] != null)
                {
                    strLanguage = context.Session[strKeyName].ToString();
                    if (!string.IsNullOrEmpty(strLanguageFromQueryString) && strLanguageFromQueryString != strLanguage)
                    {
                        strLanguage = strLanguageFromQueryString;
                        context.Session[strKeyName] = strLanguage;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(strLanguageFromQueryString))
                    {
                        strLanguage = strLanguageFromQueryString;
                    }

                    if (context.Session != null)
                    {
                        context.Session[strKeyName] = strLanguage;
                    }
                }

                var lang = context.Request.Headers["Accept-Language"];
                if (lang != null)
                {
                    if (!lang.Contains(strLanguage))
                    {
                        context.Request.Headers["Accept-Language"] = strLanguage + "," + context.Request.Headers["Accept-Language"];
                    }

                    var culture = new CultureInfo(strLanguage);

                    // Apply the culture.
                    SPUtility.SetThreadCulture(culture, culture);
                }
            }
            catch (Exception ex)
            {
                ULSLogging.LogError(ex);
            }
        }
Exemple #5
0
 public static SPUser EnsureUser(string userName, SPWeb currentWeb)
 {
     try
     {
         return(EnsureUser(userName, currentWeb.Site.Url));
     }
     catch (Exception ex)
     {
         ULSLogging.LogError(ex);
     }
     return(null);
 }
Exemple #6
0
        /// <summary>
        ///     Register even receiver for content Type
        /// </summary>
        /// <param name="contentType">Content type need to register</param>
        /// <param name="eventReceiverType">Type of Content Type</param>
        /// <param name="receiverClassType">Type of class event receiver</param>
        public static void RegisterEventReceiver(SPContentType contentType, SPEventReceiverType eventReceiverType, Type receiverClassType)
        {
            if (contentType == null)
            {
                return;
            }

            try
            {
                // unRegister Event Receiver first
                UnRegisterEventReceiver(contentType, eventReceiverType);
                contentType.EventReceivers.Add(eventReceiverType, receiverClassType.Assembly.FullName, receiverClassType.FullName);
                contentType.Update(true);
            }
            catch (Exception ex)
            {
                ULSLogging.LogError(ex);
            }
        }
Exemple #7
0
        /// <summary>
        ///     Get Value of Lookup Field
        /// </summary>
        /// <param name="fldLookup">Field Lookup</param>
        /// <returns>string value</returns>
        public static string GetValueFromLookupField(object fldLookup)
        {
            if (fldLookup == null)
            {
                return(string.Empty);
            }

            try
            {
                var lookupValue = new SPFieldLookupValue(fldLookup.ToString());
                return(lookupValue.LookupValue ?? string.Empty);
            }
            catch (Exception ex)
            {
                ULSLogging.LogError(ex);
            }

            return(string.Empty);
        }
Exemple #8
0
 private static void SendMail(string siteURL, string subject, string toAddress, bool appendHtmlTag, bool htmlEncode, string message)
 {
     SPSecurity.RunWithElevatedPrivileges(delegate()
     {
         using (SPSite site = new SPSite(siteURL))
         {
             using (SPWeb web = site.OpenWeb())
             {
                 try
                 {
                     SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);
                 }
                 catch (Exception ex)
                 {
                     ULSLogging.Log(new SPDiagnosticsCategory("STADA - SendEmailActivity - SendMail",
                                                              TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected,
                                    string.Format(CultureInfo.InvariantCulture, "{0}:{1}", ex.Message, ex.StackTrace));
                 }
             }
         }
     });
 }
Exemple #9
0
        /// <summary>
        ///     UnRegister Event Receiver
        /// </summary>
        /// <param name="contentType">Content Type need to unregister</param>
        /// <param name="eventReceiverType">Receiver Type</param>
        public static void UnRegisterEventReceiver(SPContentType contentType, SPEventReceiverType eventReceiverType)
        {
            if (contentType == null)
            {
                return;
            }

            try
            {
                for (var i = 0; i < contentType.EventReceivers.Count; i++)
                {
                    var receiver = contentType.EventReceivers[i];
                    if (receiver.Type.Equals(eventReceiverType))
                    {
                        receiver.Delete();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ULSLogging.LogError(ex);
            }
        }
Exemple #10
0
 protected void OnEndRequest(object sender, EventArgs e)
 {
     // End log:
     ULSLogging.LogMessageToFile($"-- END RequestList at: {DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss.ff")}");
 }
Exemple #11
0
 protected void OnBeginRequest(object sender, EventArgs e)
 {
     // Begin log:
     ULSLogging.LogMessageToFile($"-- BEGIN ListBaseUserUserControl at: {DateTime.Now}");
 }