/// <summary>
        /// Determines whether the specified field is editable or not.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        internal static string IsFieldEditable(string data)
        {
            try
            {
                var result = new XDocument();
                result.Add(new XElement("FieldInfo"));

                int    itemId;
                Guid   listId;
                Guid   webId;
                Guid   siteId;
                string siteUrl;

                XElement fieldInfoElement = GetFieldInfoElement(data);
                Utils.GetItemListWebSite(data, fieldInfoElement, out itemId, out listId, out webId, out siteId, out siteUrl);

                using (var spSite = new SPSite(siteUrl))
                {
                    using (SPWeb spWeb = spSite.OpenWeb(webId))
                    {
                        SPList spList = spWeb.Lists[listId];

                        SPListItem spListItem = spList.GetItemById(itemId);

                        Dictionary <string, Dictionary <string, string> > fieldProperties = Utils.GetFieldProperties(spList);

                        result.Element("FieldInfo").Add(new XElement("Fields"));

                        XElement xElement = result.Element("FieldInfo").Element("Fields");
                        xElement.Add(new XAttribute("ItemID", itemId));
                        xElement.Add(new XAttribute("ListID", listId));
                        xElement.Add(new XAttribute("WebID", webId));
                        xElement.Add(new XAttribute("SiteID", siteId));
                        xElement.Add(new XAttribute("SiteURL", siteUrl));

                        bool guessOriginalFieldNameSetting = GetGuessOriginalFieldNameSetting(fieldInfoElement);

                        foreach (string field in GetFields(fieldInfoElement))
                        {
                            string theField = field;
                            if (guessOriginalFieldNameSetting)
                            {
                                theField = Utils.ToOriginalFieldName(theField);
                            }

                            if (!spListItem.Fields.ContainsFieldWithInternalName(theField))
                            {
                                continue;
                            }

                            var element = new XElement("Field");
                            element.Add(new XAttribute("Name", theField));
                            element.Add(new XAttribute("Editable", EditableFieldDisplay.isEditable(spListItem, spListItem.Fields.GetFieldByInternalName(theField), fieldProperties)));

                            xElement.Add(element);
                        }
                    }
                }

                return(result.ToString());
            }
            catch (APIException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new APIException(6000, e.Message);
            }
        }
Exemple #2
0
        /// <summary>
        ///     Updates the list item.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        internal static string UpdateListItem(string data)
        {
            try
            {
                var result = new XDocument();
                result.Add(new XElement("ListItems"));

                foreach (XElement xElement in GetListItemElements(data))
                {
                    var listItemElement = new XElement("ListItem");

                    int    itemId;
                    Guid   listId;
                    Guid   webId;
                    Guid   siteId;
                    string siteUrl;

                    Utils.GetItemListWebSite(data, xElement, out itemId, out listId, out webId, out siteId, out siteUrl);

                    Utils.AddItemListWebSiteToXElement(itemId, listId, webId, siteId, siteUrl, ref listItemElement);

                    listItemElement.Add(new XElement("Messages"));
                    XElement messagesElement = listItemElement.Element("Messages");

                    try
                    {
                        SPWeb              web         = SPContext.Current.Web;
                        SPUser             currentUser = web.CurrentUser;
                        SPRegionalSettings currentContextRegionalSettings = currentUser.RegionalSettings ??
                                                                            web.RegionalSettings;

                        Dictionary <string, string> fieldValues = Utils.GetFieldValues(xElement);

                        using (
                            var spSite = new SPSite(siteUrl, web.Site.RootWeb.AllUsers[currentUser.LoginName].UserToken)
                            )
                        {
                            using (SPWeb spWeb = spSite.OpenWeb(webId))
                            {
                                SPRegionalSettings spRegionalSettings = spWeb.CurrentUser.RegionalSettings ??
                                                                        spWeb.RegionalSettings;

                                spWeb.AllowUnsafeUpdates = true;

                                SPList spList = spWeb.Lists[listId];

                                SPListItem spListItem = spList.GetItemById(itemId);

                                Dictionary <string, Dictionary <string, string> > fieldProperties =
                                    Utils.GetFieldProperties(spList);

                                #region Update List Item

                                foreach (var fieldValue in fieldValues)
                                {
                                    string theField = fieldValue.Key;
                                    string theValue = fieldValue.Value;

                                    if (!spList.Fields.ContainsFieldWithInternalName(theField))
                                    {
                                        messagesElement.Add(new XElement("Message",
                                                                         new XCData(
                                                                             string.Format(
                                                                                 "Cannot find a field with internal name: {0} in the list.",
                                                                                 theField))));
                                        continue;
                                    }

                                    if (spList.Fields.GetFieldByInternalName(theField).ReadOnlyField)
                                    {
                                        messagesElement.Add(new XElement("Message",
                                                                         new XCData(
                                                                             string.Format(
                                                                                 "{0} is a read-only field. Value will not be updated.",
                                                                                 theField))));
                                        continue;
                                    }

                                    if (!theField.Equals("Complete") && !theField.Equals("CommentCount") &&
                                        !theField.Equals("Commenters") && !theField.Equals("CommentersRead"))
                                    {
                                        if (
                                            !EditableFieldDisplay.isEditable(spListItem,
                                                                             spListItem.Fields.GetFieldByInternalName(
                                                                                 theField), fieldProperties))
                                        {
                                            messagesElement.Add(new XElement("Message",
                                                                             new XCData(
                                                                                 string.Format(
                                                                                     "{0} is not an editable field. The value for {0} will not be updated.",
                                                                                     theField))));
                                            continue;
                                        }
                                    }

                                    SPField spField = spListItem.Fields.GetFieldByInternalName(theField);
                                    if (spField.Type == SPFieldType.DateTime)
                                    {
                                        try
                                        {
                                            var dateTime =
                                                new DateTime((Convert.ToInt64(theValue) * 10000) + 621355968000000000);
                                            DateTime localTimeToUtc =
                                                currentContextRegionalSettings.TimeZone.LocalTimeToUTC(dateTime);
                                            DateTime utcToLocalTime =
                                                spRegionalSettings.TimeZone.UTCToLocalTime(localTimeToUtc);

                                            theValue = utcToLocalTime.ToString(CultureInfo.InvariantCulture);
                                        }
                                        catch
                                        {
                                        }
                                    }

                                    spListItem[theField] = theValue;
                                }

                                #endregion

                                spListItem.Update();
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        messagesElement.Add(new XElement("Message", new XCData(exception.Message)));
                    }

                    result.Element("ListItems").Add(listItemElement);
                }

                return(result.ToString());
            }
            catch (APIException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new APIException(6050, e.Message);
            }
        }