示例#1
0
        public ObjectInstance GetOwner()
        {
            ObjectInstance result = null;

            var owner = m_group.Owner;

            if (owner is SPUser)
            {
                result = new SPUserInstance(Engine, (owner as SPUser));
            }
            else if (owner is SPGroup)
            {
                result = new SPGroupInstance(Engine, (owner as SPGroup));
            }

            return(result);
        }
        public static ObjectInstance GetFieldValuesAsObject(ScriptEngine engine, SPListItem listItem)
        {
            var result = engine.Object.Construct();

            var fields = listItem.Fields;

            foreach (var field in fields.OfType <SPField>())
            {
                switch (field.Type)
                {
                case SPFieldType.Integer:
                {
                    int value;
                    if (listItem.TryGetSPFieldValue(field.Id, out value))
                    {
                        result.SetPropertyValue(field.InternalName, value, false);
                    }
                }
                break;

                case SPFieldType.Boolean:
                {
                    bool value;
                    if (listItem.TryGetSPFieldValue(field.Id, out value))
                    {
                        result.SetPropertyValue(field.InternalName, value, false);
                    }
                }
                break;

                case SPFieldType.Number:
                {
                    double value;
                    if (listItem.TryGetSPFieldValue(field.Id, out value))
                    {
                        result.SetPropertyValue(field.InternalName, value, false);
                    }
                }
                break;

                case SPFieldType.DateTime:
                {
                    DateTime value;
                    if (listItem.TryGetSPFieldValue(field.Id, out value))
                    {
                        result.SetPropertyValue(field.InternalName, JurassicHelper.ToDateInstance(engine, new DateTime(value.Ticks, DateTimeKind.Local)), false);
                    }
                }
                break;

                case SPFieldType.URL:
                {
                    string urlFieldValue;
                    if (listItem.TryGetSPFieldValue(field.Id, out urlFieldValue))
                    {
                        var urlValue = new SPFieldUrlValue(urlFieldValue);

                        var item = engine.Object.Construct();
                        item.SetPropertyValue("description", urlValue.Description, false);
                        item.SetPropertyValue("url", urlValue.Url, false);

                        result.SetPropertyValue(field.InternalName, item, false);
                    }
                }
                break;

                case SPFieldType.User:
                {
                    string userToken;
                    if (listItem.TryGetSPFieldValue(field.Id, out userToken))
                    {
                        var fieldUserValue = new SPFieldUserValue(listItem.ParentList.ParentWeb, userToken);
                        var userInstance   = new SPUserInstance(engine, fieldUserValue.User);
                        result.SetPropertyValue(field.InternalName, userInstance, false);
                    }
                }
                break;

                case SPFieldType.Lookup:
                {
                    var fieldType = field as SPFieldLookup;
                    if (fieldType == null)
                    {
                        continue;
                    }

                    if (fieldType.AllowMultipleValues)
                    {
                        object fv;
                        if (!listItem.TryGetSPFieldValue(field.Id, out fv))
                        {
                            continue;
                        }

                        var fieldValue = fv as SPFieldLookupValueCollection;

                        var array = engine.Array.Construct();

                        if (fieldValue != null)
                        {
                            foreach (var lookupValue in fieldValue)
                            {
                                var item = engine.Object.Construct();
                                item.SetPropertyValue("lookupId", lookupValue.LookupId, false);
                                item.SetPropertyValue("lookupValue", lookupValue.LookupValue, false);

                                ArrayInstance.Push(array, item);
                            }
                        }

                        result.SetPropertyValue(field.InternalName, array, false);
                    }
                    else
                    {
                        object fieldValue;
                        if (!listItem.TryGetSPFieldValue(field.Id, out fieldValue))
                        {
                            continue;
                        }

                        if (fieldValue is SPFieldUrlValue)
                        {
                            var urlValue = fieldValue as SPFieldUrlValue;
                            var item     = engine.Object.Construct();
                            item.SetPropertyValue("description", urlValue.Description, false);
                            item.SetPropertyValue("url", urlValue.Url, false);

                            result.SetPropertyValue(field.InternalName, item, false);
                        }
                        else if (fieldValue is DateTime)
                        {
                            var value = (DateTime)fieldValue;
                            result.SetPropertyValue(field.InternalName, JurassicHelper.ToDateInstance(engine, new DateTime(value.Ticks, DateTimeKind.Local)), false);
                        }
                        else if (fieldValue is SPFieldUserValue)
                        {
                            var fieldUserValue = (SPFieldUserValue)fieldValue;
                            var userInstance   = new SPUserInstance(engine, fieldUserValue.User);
                            result.SetPropertyValue(field.InternalName, userInstance, false);
                        }
                        else if (fieldValue is Guid)
                        {
                            var guidValue    = (Guid)fieldValue;
                            var guidInstance = new GuidInstance(engine.Object.InstancePrototype, guidValue);
                            result.SetPropertyValue(field.InternalName, guidInstance, false);
                        }
                        else if (fieldValue is string)
                        {
                            //Attempt to create a new SPFieldLookupValue from the string
                            if (DigitRegex.IsMatch((string)fieldValue, 0))
                            {
                                try
                                {
                                    var lookupValue = new SPFieldLookupValue((string)fieldValue);

                                    var item = engine.Object.Construct();
                                    item.SetPropertyValue("lookupId", lookupValue.LookupId, false);
                                    item.SetPropertyValue("lookupValue", lookupValue.LookupValue, false);
                                    result.SetPropertyValue(field.InternalName, item, false);
                                }
                                catch (ArgumentException)
                                {
                                    result.SetPropertyValue(field.InternalName, fieldValue, false);
                                }
                            }
                            else
                            {
                                result.SetPropertyValue(field.InternalName, fieldValue, false);
                            }
                        }
                        else
                        {
                            result.SetPropertyValue(field.InternalName, fieldValue, false);
                        }
                    }
                }
                break;

                default:
                {
                    object value;
                    if (listItem.TryGetSPFieldValue(field.Id, out value))
                    {
                        var stringValue = field.GetFieldValueAsText(value);

                        if (result.HasProperty(field.InternalName) == false)
                        {
                            result.SetPropertyValue(field.InternalName, stringValue, false);
                        }
                    }
                }
                break;
                }
            }

            return(result);
        }
示例#3
0
        public static object GetListItemVersionObject(ScriptEngine engine, SPListItemVersion version, SPField field)
        {
            switch (field.Type)
            {
            case SPFieldType.Integer:
            {
                int value;
                if (version.TryGetSPFieldValue(field.InternalName, out value))
                {
                    return(value);
                }
            }
            break;

            case SPFieldType.Boolean:
            {
                bool value;
                if (version.TryGetSPFieldValue(field.InternalName, out value))
                {
                    return(value);
                }
            }
            break;

            case SPFieldType.Number:
            {
                double value;
                if (version.TryGetSPFieldValue(field.InternalName, out value))
                {
                    return(value);
                }
            }
            break;

            case SPFieldType.DateTime:
            {
                DateTime value;
                if (version.TryGetSPFieldValue(field.InternalName, out value))
                {
                    return(JurassicHelper.ToDateInstance(engine, new DateTime(value.Ticks, DateTimeKind.Local)));
                }
            }
            break;

            case SPFieldType.URL:
            {
                string urlFieldValue;
                if (version.TryGetSPFieldValue(field.InternalName, out urlFieldValue))
                {
                    var urlValue = new SPFieldUrlValue(urlFieldValue);

                    var item = engine.Object.Construct();
                    item.SetPropertyValue("description", urlValue.Description, false);
                    item.SetPropertyValue("url", urlValue.Url, false);

                    return(item);
                }
            }
            break;

            case SPFieldType.User:
            {
                string userToken;
                if (version.TryGetSPFieldValue(field.InternalName, out userToken))
                {
                    var fieldUserValue = new SPFieldUserValue(version.ListItem.Web, userToken);
                    var userInstance   = new SPUserInstance(engine, fieldUserValue.User);
                    return(userInstance);
                }
            }
            break;

            case SPFieldType.Lookup:
            {
                var fieldType = field as SPFieldLookup;
                if (fieldType == null)
                {
                    return(null);
                }

                if (fieldType.AllowMultipleValues)
                {
                    object fv;
                    if (!version.TryGetSPFieldValue(field.InternalName, out fv))
                    {
                        return(null);
                    }

                    var fieldValue = fv as SPFieldLookupValueCollection;

                    var array = engine.Array.Construct();

                    if (fieldValue != null)
                    {
                        foreach (var lookupValue in fieldValue)
                        {
                            var item = engine.Object.Construct();
                            item.SetPropertyValue("lookupId", lookupValue.LookupId, false);
                            item.SetPropertyValue("lookupValue", lookupValue.LookupValue, false);

                            ArrayInstance.Push(array, item);
                        }
                    }

                    return(array);
                }
                else
                {
                    object fieldValue;
                    if (!version.TryGetSPFieldValue(field.InternalName, out fieldValue))
                    {
                        return(null);
                    }

                    var fieldUrlValue = fieldValue as SPFieldUrlValue;
                    if (fieldUrlValue != null)
                    {
                        var urlValue = fieldUrlValue;
                        var item     = engine.Object.Construct();
                        item.SetPropertyValue("description", urlValue.Description, false);
                        item.SetPropertyValue("url", urlValue.Url, false);

                        return(item);
                    }

                    if (fieldValue is DateTime)
                    {
                        var value = (DateTime)fieldValue;
                        return(JurassicHelper.ToDateInstance(engine,
                                                             new DateTime(value.Ticks, DateTimeKind.Local)));
                    }

                    var userValue = fieldValue as SPFieldUserValue;
                    if (userValue != null)
                    {
                        var fieldUserValue = userValue;
                        var userInstance   = new SPUserInstance(engine, fieldUserValue.User);
                        return(userInstance);
                    }

                    if (fieldValue is Guid)
                    {
                        var guidValue    = (Guid)fieldValue;
                        var guidInstance = new GuidInstance(engine.Object.InstancePrototype, guidValue);
                        return(guidInstance);
                    }

                    var s = fieldValue as string;
                    if (s == null)
                    {
                        return(fieldValue);
                    }

                    //Attempt to create a new SPFieldLookupValue from the string
                    if (!DigitRegex.IsMatch(s, 0))
                    {
                        return(fieldValue);
                    }

                    try
                    {
                        var lookupValue = new SPFieldLookupValue(s);

                        var item = engine.Object.Construct();
                        item.SetPropertyValue("lookupId", lookupValue.LookupId, false);
                        item.SetPropertyValue("lookupValue", lookupValue.LookupValue, false);
                        return(item);
                    }
                    catch (ArgumentException)
                    {
                        return(fieldValue);
                    }
                }
            }

            default:
            {
                object value;
                if (version.TryGetSPFieldValue(field.InternalName, out value))
                {
                    var stringValue = field.GetFieldValueAsText(value);

                    return(stringValue);
                }
            }
            break;
            }

            return(null);
        }